May 25th, 2020
Written by Brahm Capoor, Juliette Woodrow & Kara Eng
Implement the following function:
def map_students_to_dorms(all_housing_assignments):
"""
Given a list of length-2 tuples whose first elements are student
names and whose second elements are dorm names, create and return
a dictionary which associates each student with a list of all the
dorms they lived in throughout their time at Stanford.
all_housing_assignments contains information about undergraduates
in every year, so some students might only have one residence whilst
others might have multiple.
>>> all_housing_assignments = [
("Brahm", "Larkin"),
("Brahm", "Roble"),
("Brahm", "Mirrielees")
("Brahm", "Mars"),
("Brahm", "Jerry"),
("Chris", "FroSoCo"),
("Chris", "Kimball"),
("Chris", "Toyon"),
("Chris", "Roble"),
("Mehran", "Paloma"),
("Mehran", "Roble"),
("Mehran", "Loro"),
("Mehran", "Soto")
]
>>> map_students_to_dorms(all_housing_assignments)
{'Brahm': ['Larkin', 'Roble', 'Mirrielees', 'Mars', 'Jerry'], 'Chris': ['FroSoCo', 'Kimball', 'Toyon', 'Roble'], 'Mehran': ['Paloma', 'Roble', 'Loro', 'Soto']}
"""
Fun fact: these were actually the dorms Brahm, Mehran and Chris lived in at Stanford!
In last week's section, you implemented a program that constructs what we refer to as a user tags dictionary. This dictionary maps twitter handles to dictionaries which map hashtags to frequencies of usage. For example, suppose we have the following user tags dictionary:
{'@kanyewest': {'yeezy': 50, 'chicago': 20, 'pablo': 30}}
This dictionary illustrates the unlikely scenario in which
@kanyewest
is the only user of twitter (since that is the
only key in the outer dictionary) and that he has tweeted about
yeezy
, chicago
and pablo
50, 20 and
30 times respectively.
Our goal is to implement a program to visualize this data for us, by
showing the relative usage of each hashtag by a user. For the dictionary
associated with @kanyewest
, this visualization would look
like this:
In order to produce this visualization, you should implement the following function:
def visualize_trends(canvas, user_tags, user_name):
"""
Draws a visualization of the top 10 hashtags used
by the user whose handle is user_name.
"""
As you write this function, keep in mind the following details:
user_tags
dictionary.
The starter code loads this dictionary in from a JSON file and passes it
into your function.
get_top_tags
, which
accepts as a parameter a
single user's hashtag frequency dictionary, and returns a
frequency dictionary of just the TOP_N
most used hashtags,
where TOP_N
is a constant defined to be equal to 10.
@kanyewest
's tweets used the chicago
hashtag,
and so the bar for chicago
is 20% the height of the canvas.
Restated, the height of all the bars combined should equal
CANVAS_HEIGHT
.
The design and decomposition of this function is up to you, but we suggest the following milestones:
get_top_tags
function. For each hashtag, plot a bar with
the correct position and size.
Your job here is to design a Circle
class in
circle.py
for use in the following program:
# import the Circle class from circle.py
from circle import Circle
def main():
# construct circle with radius 5
circle = Circle (5)
# print the area of the circle
print("The area of the circle is " + str(circle.get_area()))
# print the circumference of the circle
print("The circumference of the circle is " + str(circle.get_circumference()))
if __name__ == "__main__":
main()
When circle_test.py
is run, it produces the following output
(although the calculated area and circumference might be slightly
different, depending on your computer):
$ python3 circle_test.py
The area of the circle is 78.53981633974483
The circumference of the circle is 31.41592653589793
As a reminder, a circle with radius $r$ has area $\pi r^2$ and
circumference $2\pi r$. The value of $\pi$ is stored in the constant
math.pi
, which you can access by importing the
math
module.