From 51e76315fae1ea36a4186b3b75ed41da1753280b Mon Sep 17 00:00:00 2001 From: grace-xing6 Date: Wed, 16 Oct 2024 20:29:12 -0400 Subject: [PATCH] friend_fcuntion I was stuck on def people_who_like_color(people, color) and def get_color_dict(people); I forgot to define the list or dict first; Or sometimes I used the wrong label for them. I checked the output when I test it and also ask ChatGPT to identify what's wrong with my code. --- friend_functions.py | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/friend_functions.py b/friend_functions.py index 6f5ecf7..cd2cfee 100644 --- a/friend_functions.py +++ b/friend_functions.py @@ -17,7 +17,10 @@ def count_people(people): >>> count_people(friends) 10 """ - raise NotImplementedError() + n = 0 + for i in people: + n = n + 1 + return n def get_email(people, name): """Returns the named person's email address. If there is no such person, returns None. @@ -27,7 +30,10 @@ def get_email(people, name): >>> get_email(friends, "Tad Winters") None """ - raise NotImplementedError() + for person in people: + if person["name"] == 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 +43,10 @@ def count_favorite_colors(people, name): >>> count_favorite_colors(family, "Raphael Chambers") 1 """ - raise NotImplementedError() + for person in people: + if person["name"] == name: + return len(person["favorite_colors"]) + return None # If the person is not found def people_who_like_color(people, 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") [] """ - raise NotImplementedError() + matching_people = [] + for person in people: + if color in person["favorite_colors"]: + matching_people.append(person) + return matching_people def count_people_who_like_color(people, color): """Returns the number of people who like a given color. @@ -67,7 +80,13 @@ def count_people_who_like_color(people, color): >>> count_people_who_like_color(family, "orange") 1 """ - raise NotImplementedError() + n = 0 + for person in people: + for colors in person["favorite_colors"]: + if colors == color: + n = n + 1 + return n + def get_color_dict(people): """Returns a dict showing how many people like each color. @@ -86,7 +105,17 @@ def get_color_dict(people): "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 + +