Checkpoint 1

I was stuck on the very first one because I was trying to use the count
command that we used in the pipes lab. For some reason it wasn't working so
I had to do some research and discovered the len() command that works
in scenarios like this. I was then able to use this command on
some of the other problems.
This commit is contained in:
Lauren Dawnkaski 2024-10-17 09:52:29 -04:00
parent b40d0314ca
commit f6279ba4eb
1 changed files with 30 additions and 7 deletions

View File

@ -17,7 +17,7 @@ def count_people(people):
>>> count_people(friends) >>> count_people(friends)
10 10
""" """
raise NotImplementedError() return len(people)
def get_email(people, name): def get_email(people, name):
"""Returns the named person's email address. If there is no such person, returns None. """Returns the named person's email address. If there is no such person, returns None.
@ -27,7 +27,11 @@ def get_email(people, name):
>>> get_email(friends, "Tad Winters") >>> get_email(friends, "Tad Winters")
None None
""" """
raise NotImplementedError() for person in people:
if person["name"]==name:
return person["email"]
else:
return None
def count_favorite_colors(people, name): def count_favorite_colors(people, name):
"""Returns the number of colors liked by the named person. If there is no such person, returns None. """Returns the number of colors liked by the named person. If there is no such person, returns None.
@ -37,7 +41,12 @@ def count_favorite_colors(people, name):
>>> count_favorite_colors(family, "Raphael Chambers") >>> count_favorite_colors(family, "Raphael Chambers")
1 1
""" """
raise NotImplementedError() for person in people:
if person["name"]==name:
return len(person["favorite_colors"])
else:
return None
def people_who_like_color(people, color): def people_who_like_color(people, color):
"""Returns a list containing only those people who like the given color. """Returns a list containing only those people who like the given color.
@ -57,7 +66,11 @@ def people_who_like_color(people, color):
>>> people_who_like_color(family, "indigo") >>> people_who_like_color(family, "indigo")
[] []
""" """
raise NotImplementedError() people_who_like_color=[]
for person in people:
if color in person["favorite_colors"]:
people_who_like_color.append(person)
return people_who_like_color
def count_people_who_like_color(people, color): def count_people_who_like_color(people, color):
"""Returns the number of people who like a given color. """Returns the number of people who like a given color.
@ -67,7 +80,11 @@ def count_people_who_like_color(people, color):
>>> count_people_who_like_color(family, "orange") >>> count_people_who_like_color(family, "orange")
1 1
""" """
raise NotImplementedError() people_who_like_color=[]
for person in people:
if color in person["favorite_colors"]:
people_who_like_color.append("person")
return len(people_who_like_color)
def get_color_dict(people): def get_color_dict(people):
"""Returns a dict showing how many people like each color. """Returns a dict showing how many people like each color.
@ -86,8 +103,14 @@ def get_color_dict(people):
"orange": 1, "orange": 1,
} }
""" """
raise NotImplementedError() color_dict={}
for person in people:
for color in person["favorite_colors"]:
if color in color_dict:
color_dict[color]=color_dict[color]+1
else:
color_dict[color]=1
return color_dict