Wrote codes to implement each function.

One place I got stuck was when I was trying to write get_color_dict
because I was not sure how to loop through each person and their favorite colors to count them.
 I got unstuck by going through my notes from CSE 115.
This commit is contained in:
juddin2
2025-10-12 22:38:43 -04:00
parent 90950e397a
commit eb80dc7c0b

View File

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