wrote the functions in friend_functions.py

This commit is contained in:
kated
2026-04-22 11:10:57 -04:00
parent af1c9cc388
commit d6738fe660
3 changed files with 254 additions and 6 deletions

View File

@@ -17,7 +17,8 @@ def count_people(people):
>>> count_people(friends)
10
"""
raise NotImplementedError()
x = len(people)
return x
def get_email(people, name):
"""Returns the named person's email address. If there is no such person, returns None.
@@ -27,7 +28,10 @@ def get_email(people, name):
>>> get_email(friends, "Tad Winters")
None
"""
raise NotImplementedError()
for person in people:
if name in person["name"]:
return person["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 +41,10 @@ def count_favorite_colors(people, name):
>>> count_favorite_colors(family, "Raphael Chambers")
1
"""
raise NotImplementedError()
for person in people:
if name in person["name"]:
return len(person["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 +64,11 @@ def people_who_like_color(people, color):
>>> people_who_like_color(family, "indigo")
[]
"""
raise NotImplementedError()
person_list = []
for person in people:
if color in person["favorite_colors"]:
person_list.append(person)
return person_list
def count_people_who_like_color(people, color):
"""Returns the number of people who like a given color.
@@ -67,7 +78,11 @@ def count_people_who_like_color(people, color):
>>> count_people_who_like_color(family, "orange")
1
"""
raise NotImplementedError()
person_list = []
for person in people:
if color in person["favorite_colors"]:
person_list.append(person)
return len(person_list)
def get_color_dict(people):
"""Returns a dict showing how many people like each color.
@@ -86,7 +101,14 @@ def get_color_dict(people):
"orange": 1,
}
"""
raise NotImplementedError()
fav_color_dict = {}
for person in people:
for color in person["favorite_colors"]:
if color in fav_color_dict:
fav_color_dict[color] += 1
else:
fav_color_dict[color] = 1
return fav_color_dict