February 28th, 2021
Written by Juliette Woodrow, Brahm Capoor, Anna Mistele, and John Dalloul
temps_F = [45.7, 55.3, 62.1, 75.4, 32.0, 0.0, 100.0]
#convert from Fahrenheit to Celsius
temps_C = list(map(lambda f : (f - 32) * 5 / 9, temps_F))
print('C: ', temps_C)
original_headings = [(30, 'N'), (359, 'N'), (340, 'N'), (270, 'W'), (124, 'E'), (149, 'S'), (219, 'S')]
def update_heading(reading):
updated_heading = (reading[0] + 22) % 360
updated_direction = calc_direction(updated_heading)
return (updated_heading, updated_direction)
def calc_direction(heading):
if 46 <= heading and heading <= 135:
return 'E'
elif 136 <= heading and heading <= 225:
return 'S'
elif 225 <= heading and heading <= 315:
return 'W'
else:
return 'N'
update_list = list(map(update_heading, original_headings))
print(update_list)
map(lambda word: word[::-1], text.split())
# OR
def reverse(s):
result = ""
for ch in s:
result = ch + result
return result
map(reverse, text.split())
def linear_transformation(vector, scale, shift):
"""
Takes in a vector (list of numbers), a scale factor, and a shift constant and returns the vector
where the ith term is equivalent to the ith term of the original vector multiplied by the scale
and added to the shift.
"""
# can set the lambda function to a variable to make code more readable
linear_transformation_fn = lambda x : scale * x + shift
result = map(linear_transformation_fn, vector)
return result
def apply_curve(grades):
return map(lambda x: x + (100-x) * 0.5, grades)
counting = [5, 6, 7, 8]
jenny = [8, 6, 7, 5, 3, 0]
map(lambda lst: lst.append(9), [counting, jenny])
# =>
"""
Whoa! Why does this iterable not contain anything? `map` replaces each
element with the *return* value of the function call. In this case, the
append function returns None, but modifies the value of the list... Tricky!
"""
counting = [5, 6, 7, 8]
jenny = [8, 6, 7, 5, 3, 0]
map(lambda lst: lst + [9], [counting, jenny])
# => <[5, 6, 7, 8, 9], [8, 6, 7, 5, 3, 0, 9]>
"""
This works as expected, because lst1 + lst2 *returns* a new list. In our
case, that's the original list, with the number 9 appended.
"""
>>> strs = ['apple', 'BANANA', 'candy', 'aardvark']
# 1A.
>>> sorted(strs, key=lambda word : word.lower())
['aardvark', 'apple', 'BANANA', 'candy']
# 1B.
>>> sorted(strs, key=lambda word : word.lower()[len(word)-1])
['BANANA', 'apple', 'aardvark', 'candy']
#2
>>> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> sorted(nums, key=lambda num:abs(num-3.14))
[3, 4, 2, 5, 1, 6, 7, 8, 9, 10]
>>> min(nums, key=lambda num:abs(num-3.14))
3
>>> max(nums, key=lambda num:abs(num-3.14))
10
# 3A.
>>> sorted(rentals, key=lambda room_tuple:room_tuple[1])
[('elm st.', 1, 1200), ('pine st.', 2, 1600), ('main st.', 4, 4000)]
# 3B.
>>> sorted(rentals, key=lambda room_tuple:room_tuple[2])
[('elm st.', 1, 1200), ('pine st.', 2, 1600), ('main st.', 4, 4000)]
# 3C.
>>> sorted(rentals, key=lambda room_tuple:room_tuple[2]/room_tuple[1])
[('pine st.', 2, 1600), ('main st.', 4, 4000), ('elm st.', 1, 1200)]
>>> min(rentals, key=lambda room_tuple:room_tuple[2]/room_tuple[1])
('pine st.', 2, 1600)
Part 1
def reverse_keys(flat_counts):
for pair in sorted(flat_counts.items(), reverse=True):
key = pair[0]
print(key)
def most_used(flat_counts):
"""
Given a non-empty "flat" counts dict of tags, return the tag
that has the largest count.
"""
sorted_tags = sorted(flat_counts.items(), key=lambda x: x[1], reverse=True)
print(sorted_tags[:10])