Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sebabouche/5cb9ab88ce825e49b50def2780f5657f to your computer and use it in GitHub Desktop.
Save sebabouche/5cb9ab88ce825e49b50def2780f5657f to your computer and use it in GitHub Desktop.
Exercises with map, filter, reduce without using list comprehension (which is not very pythonic but interesting).
TEMPERATURES = [37, 42, -24, 39]
def fahrenheit(T):
return ((float(9)/5)*T + 32)
def celsius(T):
return (float(5)/9)*(T-32)
print(list(map(fahrenheit, TEMPERATURES)))
print(list(map(lambda x: float(9)/5*x + 32, TEMPERATURES)))
litterature = [10.0, 11.5, 13.6, 17.3]
math = [13.4, 15.4, 17.5, 19.2]
print(list(map(lambda x, y: (x+y)/2, litterature, math)))
alphabet = ["a", "b", "c", "d"]
numerique = [1, 2, 3, 4]
print(list(map(lambda x, y, z: x*y*z, alphabet, numerique, numerique)))
from math import cos, sin, tan
def map_functions(x, functions):
return [func(x) for func in functions]
print(map_functions(1, (cos, sin, tan)))
fibonacci = [0,1,1,2,3,5,8,13,21,34,55]
print(list(filter(lambda x: x % 2 == 0, fibonacci)))
from functools import reduce
print(reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment