diff --git a/friend_functions.py b/friend_functions.py index 6f5ecf7..e207daf 100644 --- a/friend_functions.py +++ b/friend_functions.py @@ -10,24 +10,17 @@ # each and instead write code which returns the expected values. def count_people(people): - """Counts the number of people. - - >>> count_people(family) - 5 - >>> count_people(friends) - 10 - """ - raise NotImplementedError() + #Counts the number of people in the list/dict sent + count = len(people) + return count + def get_email(people, name): - """Returns the named person's email address. If there is no such person, returns None. - - >>> get_email(family, "Tad Winters") - "ligula.aenean@hotmail.edu" - >>> get_email(friends, "Tad Winters") - None - """ - raise NotImplementedError() + #Returns the named person's email address. If there is no such person, returns None. + 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. @@ -37,7 +30,11 @@ def count_favorite_colors(people, name): >>> count_favorite_colors(family, "Raphael Chambers") 1 """ - raise NotImplementedError() + for person in people: + if person.get("name") == name: + fav_colors = len(person.get("favorite_colors")) + return fav_colors + return None def people_who_like_color(people, color): """Returns a list containing only those people who like the given color. @@ -57,7 +54,8 @@ def people_who_like_color(people, color): >>> people_who_like_color(family, "indigo") [] """ - raise NotImplementedError() + return [person for person in people if color in person.get("favorite_colors", [])] + def count_people_who_like_color(people, color): """Returns the number of people who like a given color. @@ -67,7 +65,7 @@ def count_people_who_like_color(people, color): >>> count_people_who_like_color(family, "orange") 1 """ - raise NotImplementedError() + return len(people_who_like_color(people,color)) def get_color_dict(people): """Returns a dict showing how many people like each color. @@ -86,7 +84,14 @@ def get_color_dict(people): "orange": 1, } """ - raise NotImplementedError() + fav_color_dict = {} + for person in people: + for color in person.get("favorite_colors", []): + if color in fav_color_dict: + fav_color_dict[color] += 1 + else: + fav_color_dict[color] = 1 + return fav_color_dict diff --git a/people.py b/people.py index 68ea27e..ff04d4e 100644 --- a/people.py +++ b/people.py @@ -36,7 +36,7 @@ family = [ friends = [ { - "name": "Connor Rodriguez", + "name": "Connor Rodriguez", "email": "maecenas@yahoo.edu", "favorite_colors": ["aqua", "teal", "sea foam", "turquoise"], },