From 118c219d0902d48f4410aa3cebe49209f36b4067 Mon Sep 17 00:00:00 2001 From: njmason2 Date: Sat, 18 Oct 2025 15:57:08 -0400 Subject: [PATCH] friend_functions.py --- friend_functions.py | 63 ++++++++++++++++++++++------------------ test_friend_functions.py | 30 +++++++++---------- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/friend_functions.py b/friend_functions.py index af290f0..00bce1a 100644 --- a/friend_functions.py +++ b/friend_functions.py @@ -48,39 +48,36 @@ # if (person["name"]==name): # for a dict within a list lookup # return len(person["favorite_colors"]) # count of list elements in a dict within a list -# return None # reached the end of the list without a match - -def people_who_like_color(people, color): - """Returns a list containing only those people who like the given color. - >>> people_who_like_color(family, "yellow") - [ - { - "name": "Walker Hurley", - "email": "auctor.odio@icloud.ca", - "favorite_colors": ["red", "yellow", "blue", "orange"], - }, - { - "name": "Clementine Joseph", - "email": "hendrerit@aol.co.uk", - "favorite_colors": ["yellow", "aqua", "black"], - } - ] - >>> people_who_like_color(family, "indigo") - [] - """ - fav_name=[] # initialize output list as None +# def people_who_like_color(people, color): +# """Returns a list containing only those people who like the given color. +# >>> people_who_like_color(family, "yellow") +# [ +# { +# "name": "Walker Hurley", +# "email": "auctor.odio@icloud.ca", +# "favorite_colors": ["red", "yellow", "blue", "orange"], +# }, +# { +# "name": "Clementine Joseph", +# "email": "hendrerit@aol.co.uk", +# "favorite_colors": ["yellow", "aqua", "black"], +# } +# ] +# >>> people_who_like_color(family, "indigo") +# [] +# """ +# 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 - - # print(fav_name) - return fav_name # at the end of file, 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 fav_name # at the end of file, output list -#def count_people_who_like_color(people, 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") @@ -88,7 +85,15 @@ def people_who_like_color(people, color): >>> count_people_who_like_color(family, "orange") 1 """ -# raise NotImplementedError() + 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 + + return len(fav_name) # count of names of matching color + + #def get_color_dict(people): """Returns a dict showing how many people like each color. diff --git a/test_friend_functions.py b/test_friend_functions.py index 7fc51d5..5ef95c0 100644 --- a/test_friend_functions.py +++ b/test_friend_functions.py @@ -12,8 +12,8 @@ from friend_functions import ( # count_people, # get_email, # count_favorite_colors, - people_who_like_color, - # count_people_who_like_color, + # people_who_like_color, + count_people_who_like_color, # get_color_dict, ) @@ -39,21 +39,21 @@ from friend_functions import ( # def test_returns_none_when_person_missing(self): # self.assertEqual(count_favorite_colors(family, "Ken Kesey"), None) -class TestPeopleWhoLikeColor(TestCase): - def test_returns_correct_length(self): - self.assertEqual(len(people_who_like_color(family, "blue")), 2) - self.assertEqual(len(people_who_like_color(family, "purple")), 0) +# class TestPeopleWhoLikeColor(TestCase): +# def test_returns_correct_length(self): +# self.assertEqual(len(people_who_like_color(family, "blue")), 2) +# self.assertEqual(len(people_who_like_color(family, "purple")), 0) - def test_returns_full_dicts(self): - result = people_who_like_color(family, "orange") - self.assertEqual(len(result), 1) - self.assertEqual(result[0]["email"], "auctor.odio@icloud.ca") +# def test_returns_full_dicts(self): +# result = people_who_like_color(family, "orange") +# 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):