diff --git a/friend_functions.py b/friend_functions.py index 6f5ecf7..212c1c6 100644 --- a/friend_functions.py +++ b/friend_functions.py @@ -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,19 +27,27 @@ def get_email(people, name): >>> get_email(friends, "Tad Winters") None """ - raise NotImplementedError() + for person in people: + if person.get("name") == name: + return person.get("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. + """Returns the number of colors liked by the named person. If there is no such person, returns None.""" + for person in people: + if person.get("name") == name: + return len(person.get("favorite_colors", [])) + return None - >>> count_favorite_colors(family, "Tad Winters") - 2 - >>> count_favorite_colors(family, "Raphael Chambers") - 1 - """ - raise NotImplementedError() def people_who_like_color(people, color): + """Returns a list containing only those people who like the given color.""" + result = [] + for person in people: + if color in person.get("favorite_colors", []): + result.append(person) + return result + """Returns a list containing only those people who like the given color. >>> people_who_like_color(family, "yellow") [ @@ -57,19 +65,25 @@ def people_who_like_color(people, color): >>> people_who_like_color(family, "indigo") [] """ - raise NotImplementedError() +"""Returns a list containing only those people who like the given color.""" +"Returns a list containing only those people who like the given color.""" def count_people_who_like_color(people, color): - """Returns the number of people who like a given color. - - >>> count_people_who_like_color(family, "red") - 2 - >>> count_people_who_like_color(family, "orange") - 1 - """ - raise NotImplementedError() + """Returns the number of people who like a given color.""" + count = 0 + for person in people: + if color in person.get("favorite_colors", []): + count += 1 + return count def get_color_dict(people): + """Returns a dict showing how many people like each color.""" + color_dict = {} + for person in people: + for color in person.get("favorite_colors", []): + color_dict[color] = color_dict.get(color, 0) + 1 + return color_dict + """Returns a dict showing how many people like each color. Any color liked by any of the people will be included, and only colors liked by someone will be included. The order of items in the dict doesn't matter. @@ -86,7 +100,8 @@ def get_color_dict(people): "orange": 1, } """ - raise NotImplementedError() +"""Returns a dict showing how many people like each color.""" +