November 7th, 2021
Written by Juliette Woodrow, Brahm Capoor, Anna Mistele, and John Dalloul
Solve each of these problems using the map function described in lecture.
You and your freshman year roommate grew up in different countries. You often have trouble trying to explain the current temperature outside as one of you is familiar with degrees Celsius and the other is used to using degrees Fahrenheit. Fortunately, one of you is taking 106A and can help!
Given a list of temperatures in degrees Farenheit like so:
temps_F = [45.7, 55.3, 62.1, 75.4, 32.0, 0.0, 100.0]
Write an expression to produce a list of those same temperatures, measured in degrees Celsius. As a reminder, to convert Farenheit to Celsius, you first subtract 32 and then multiply by 5⁄9.
s = """
gnidroccA ot lla nwonk swal fo ,noitaiva ereht si on yaw a eeb dluohs eb
elba ot .ylf stI sgniw era oot llams ot teg sti taf elttil ydob ffo eht
.dnuorg ehT ,eeb fo ,esruoc seilf yawyna esuaceb seeb t'nod erac tahw snamuh
kniht si .elbissopmi
"""
Solve each of the following challenges in one line of Python, using the lambda technique:
[('main st.', 4, 4000), ('elm st.', 1, 1200), ('pine st.', 2, 1600)]
Sort the list in the following ways:
You will need Friday's lecture to complete this problem :) Recall the Big Tweets Data problem from last week, in which we worked with a user_tags dictionary whose keys were twitter usernames and whose values were additional nested dictionaries keeping track of the frequencies of Hashtag usage, like so:
user_tags = {'@alice': {'#apple': 1, '#banana': 2}, '@bob': {'#apple': 1}}
One of the suggested extensions for this problem was to implement a function called flat_counts, which takes in a user_tags and returns a dictionary that counts the number of times each Hashtag is used, across all users. For example, calling flat_counts and passing the user_tags dictionary in as a parameter would lead to the following behaviour:
>>> flat_counts(user_tags)
{'#apple': 2, '#banana': 2}
In Python, dictionaries have a built-in items() function that returns a list of (key, value) tuples.