Download List comprehensions - MIT OpenCourseWare

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Vincent's theorem wikipedia , lookup

Halting problem wikipedia , lookup

Series (mathematics) wikipedia , lookup

Elementary mathematics wikipedia , lookup

Addition wikipedia , lookup

Transcript
Problem Wk.1.4.3: List Comprehensions
Part 1: Even Squares
Define a procedure, called evenSquares that takes a list of numbers as input and returns
a list of the squares of the input values that are even. Use a list comprehension. You
can test whether a number is even by seeing if the number mod 2 is 0, that is,
x % 2 == 0
Part 2: Sum of abs product
Use a list comprehension to define a procedure, called sumAbsProd, that returns the sum
of the absolute values of the products of all the pairs of numbers where one is drawn
from each of the two input lists.
>>> sumAbsProd([2,-3],[4,-5])
45
That is 2*4 + 2*5 + 3*4 + 3*5 = 45.
Use the sum and abs built-in functions in Python.
MIT OpenCourseWare
http://ocw.mit.edu
6.01SC Introduction to Electrical Engineering and Computer Science
Spring 2011
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.