February 28th, 2021
Written by Juliette Woodrow, Brahm Capoor, Anna Mistele, and John Dalloul
Predict what each of the following blocks of code will do:
counting = [5, 6, 7, 8]
jenny = [8, 6, 7, 5, 3, 0]
lst = list(map(lambda lst: lst.append(9), [counting, jenny]))
counting = [5, 6, 7, 8]
jenny = [8, 6, 7, 5, 3, 0]
lst = list(map(lambda lst: lst + [9], [counting, jenny]))
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.
You and some friends want to see what is going on at Cal before the big game. They might be planning something suspicous. To investigate, you plan to fly a drone over the football stadium in the days leading up to the game.
Like any responsible drone pilot, you have plotted the flight path by writing down the direction, or heading, of the plane at each turn. An hour before the first takeoff, however, you realize that you didn't account for the magnetic deviation, or the local magnetic fields created by technology on the drone that mess with its internal compass reading. You also forgot to account for magnetic variation, or the difference between true north and magentic north. To account for magnetic deviation and variation, you must add 22 degrees to all of your heading calculations. Recall that the compass wraps around at 360 degrees, so any new heading calculated must also be between 0 and 359 degrees.
Along with exact headings, you have also calculated if each turn is generally towards the North, East, South or West using the following buckets:
Your job is to write an expression that given a list like so:
original_headings = [(30, 'N'), (359, 'N'), (340, 'N'), (270, 'W'), (124, 'E'), (149, 'S'), (219, 'S')]
Produces a new list with the updated headings and directions after correcting for magnetic variation and deviation. You may implement any helper functions you want, but your top-level expression should be one line of code.
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:
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.