diff --git a/friend_functions.py b/friend_functions.py index 00bce1a..46abd9a 100644 --- a/friend_functions.py +++ b/friend_functions.py @@ -77,25 +77,25 @@ -def count_people_who_like_color(people, color): - """Returns the number of people who like a 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 - """ - fav_name=[] # initialize output list as None +# >>> count_people_who_like_color(family, "red") +# 2 +# >>> count_people_who_like_color(family, "orange") +# 1 +# """ +# fav_name=[] # initialize output list as None - for person in people: - if color in person["favorite_colors"]: # if color in the data file matches input color - fav_name.append(person) # append output list +# for person in people: +# if color in person["favorite_colors"]: # if color in the data file matches input color +# fav_name.append(person) # append output list - return len(fav_name) # count of names of matching color +# return len(fav_name) # count of names of matching color -#def get_color_dict(people): +def get_color_dict(people): """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. @@ -112,4 +112,26 @@ def count_people_who_like_color(people, color): "orange": 1, } """ -# raise NotImplementedError() + d={} + v=1 + for person in people: + for color_list in person["favorite_colors"]: + if color_list in d: # if the current color is already in the list, + +v # increment by 1. + d[color_list]=d[color_list]+v # In the same key, + # replace the existing value with the incremented value. + else: # Otherwise, the current color is not in the list. + v=1 # Reset the value, since a new color must have a value of 1. + d[color_list]=v # Append a new key with the value of 1. + return(d) + + + + + + + + + + + \ No newline at end of file diff --git a/test_friend_functions.py b/test_friend_functions.py index 5ef95c0..de80877 100644 --- a/test_friend_functions.py +++ b/test_friend_functions.py @@ -9,12 +9,12 @@ from unittest import TestCase, main from people import friends, family from friend_functions import ( - # count_people, - # get_email, - # count_favorite_colors, - # people_who_like_color, - count_people_who_like_color, - # get_color_dict, +# count_people, +# get_email, +# count_favorite_colors, +# people_who_like_color, +# count_people_who_like_color, + get_color_dict, ) # class TestCountPeople(TestCase): @@ -49,42 +49,42 @@ from friend_functions import ( # self.assertEqual(len(result), 1) # self.assertEqual(result[0]["email"], "auctor.odio@icloud.ca") -class TestCountPeopleWhoLikeColor(TestCase): - def test_returns_correct_count(self): - self.assertEqual(count_people_who_like_color(friends, "grey"), 2) - self.assertEqual(count_people_who_like_color(friends, "beige"), 1) - self.assertEqual(count_people_who_like_color(friends, "cornflower"), 0) +# class TestCountPeopleWhoLikeColor(TestCase): +# def test_returns_correct_count(self): +# self.assertEqual(count_people_who_like_color(friends, "grey"), 2) +# self.assertEqual(count_people_who_like_color(friends, "beige"), 1) +# self.assertEqual(count_people_who_like_color(friends, "cornflower"), 0) -# class TestGetColorDict(TestCase): -# def test_returns_correct_dict(self): -# familyExpected = { -# "aqua": 2, -# "red": 2, -# "blue": 2, -# "black": 2, -# "white": 1, -# "grey": 1, -# "yellow": 2, -# "orange": 1, -# } -# friendsExpected = { -# "lime": 1, -# "grey": 2, -# "sea foam": 1, -# "aqua": 2, -# "teal": 1, -# "white": 1, -# "red": 2, -# "black": 2, -# "blue": 2, -# "tan": 1, -# "sand": 1, -# "green": 1, -# "beige": 1, -# "turquoise": 1, -# "yellow": 2, -# } -# self.assertEqual(get_color_dict(family), familyExpected) -# self.assertEqual(get_color_dict(friends), friendsExpected) +class TestGetColorDict(TestCase): + def test_returns_correct_dict(self): + familyExpected = { + "aqua": 2, + "red": 2, + "blue": 2, + "black": 2, + "white": 1, + "grey": 1, + "yellow": 2, + "orange": 1, + } + friendsExpected = { + "lime": 1, + "grey": 2, + "sea foam": 1, + "aqua": 2, + "teal": 1, + "white": 1, + "red": 2, + "black": 2, + "blue": 2, + "tan": 1, + "sand": 1, + "green": 1, + "beige": 1, + "turquoise": 1, + "yellow": 2, + } + self.assertEqual(get_color_dict(family), familyExpected) + self.assertEqual(get_color_dict(friends), friendsExpected) main()