friend_functions.py

This commit is contained in:
njmason2
2025-10-18 11:22:38 -04:00
parent a5e214b29f
commit 271b54e569
2 changed files with 39 additions and 30 deletions

View File

@@ -36,22 +36,22 @@
# return None # reached the end of the list without a match # return None # reached the end of the list without a match
def count_favorite_colors(people, name): # 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.
>>> count_favorite_colors(family, "Tad Winters") # >>> count_favorite_colors(family, "Tad Winters")
2 # 2
>>> count_favorite_colors(family, "Raphael Chambers") # >>> count_favorite_colors(family, "Raphael Chambers")
1 # 1
""" # """
for person in people: # for person in people:
if (person["name"]==name): # for a dict within a list lookup # 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 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 # return None # reached the end of the list without a match
#def people_who_like_color(people, color): def people_who_like_color(people, color):
"""Returns a list containing only those people who like the given color. """Returns a list containing only those people who like the given color.
>>> people_who_like_color(family, "yellow") >>> people_who_like_color(family, "yellow")
[ [
@@ -69,7 +69,16 @@ def count_favorite_colors(people, name):
>>> people_who_like_color(family, "indigo") >>> people_who_like_color(family, "indigo")
[] []
""" """
# 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
# print(fav_name)
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. """Returns the number of people who like a given color.

View File

@@ -11,8 +11,8 @@ from people import friends, family
from friend_functions import ( from friend_functions import (
# count_people, # count_people,
# get_email, # get_email,
count_favorite_colors, # count_favorite_colors,
# people_who_like_color, people_who_like_color,
# count_people_who_like_color, # count_people_who_like_color,
# get_color_dict, # get_color_dict,
) )
@@ -30,24 +30,24 @@ from friend_functions import (
# def test_returns_none_when_person_missing(self): # def test_returns_none_when_person_missing(self):
# self.assertEqual(get_email(family, "Ken Kesey"), None) # self.assertEqual(get_email(family, "Ken Kesey"), None)
class TestCountFavoriteColors(TestCase): # class TestCountFavoriteColors(TestCase):
def test_returns_len_favorite_colors_when_person_found(self): # def test_returns_len_favorite_colors_when_person_found(self):
self.assertEqual(count_favorite_colors(family, "Tad Winters"), 2) # self.assertEqual(count_favorite_colors(family, "Tad Winters"), 2)
self.assertEqual(count_favorite_colors(friends,"Connor Rodriguez"), 4) # self.assertEqual(count_favorite_colors(friends,"Connor Rodriguez"), 4)
self.assertEqual(count_favorite_colors(friends, "Yuli Reynolds"), 0) # self.assertEqual(count_favorite_colors(friends, "Yuli Reynolds"), 0)
def test_returns_none_when_person_missing(self): # def test_returns_none_when_person_missing(self):
self.assertEqual(count_favorite_colors(family, "Ken Kesey"), None) # self.assertEqual(count_favorite_colors(family, "Ken Kesey"), None)
# class TestPeopleWhoLikeColor(TestCase): class TestPeopleWhoLikeColor(TestCase):
# def test_returns_correct_length(self): def test_returns_correct_length(self):
# self.assertEqual(len(people_who_like_color(family, "blue")), 2) self.assertEqual(len(people_who_like_color(family, "blue")), 2)
# self.assertEqual(len(people_who_like_color(family, "purple")), 0) self.assertEqual(len(people_who_like_color(family, "purple")), 0)
# def test_returns_full_dicts(self): def test_returns_full_dicts(self):
# result = people_who_like_color(family, "orange") result = people_who_like_color(family, "orange")
# self.assertEqual(len(result), 1) self.assertEqual(len(result), 1)
# self.assertEqual(result[0]["email"], "auctor.odio@icloud.ca") self.assertEqual(result[0]["email"], "auctor.odio@icloud.ca")
# class TestCountPeopleWhoLikeColor(TestCase): # class TestCountPeopleWhoLikeColor(TestCase):
# def test_returns_correct_count(self): # def test_returns_correct_count(self):