friend_functions.py

This commit is contained in:
njmason2
2025-10-18 15:57:08 -04:00
parent 271b54e569
commit 118c219d09
2 changed files with 49 additions and 44 deletions

View File

@@ -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.

View File

@@ -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):