generated from mwc/lab_weather
friend_functions.py
This commit is contained in:
@@ -77,25 +77,25 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
>>> count_people_who_like_color(family, "red")
|
# >>> count_people_who_like_color(family, "red")
|
||||||
2
|
# 2
|
||||||
>>> count_people_who_like_color(family, "orange")
|
# >>> count_people_who_like_color(family, "orange")
|
||||||
1
|
# 1
|
||||||
"""
|
# """
|
||||||
fav_name=[] # initialize output list as None
|
# fav_name=[] # initialize output list as None
|
||||||
|
|
||||||
for person in people:
|
# for person in people:
|
||||||
if color in person["favorite_colors"]: # if color in the data file matches input color
|
# if color in person["favorite_colors"]: # if color in the data file matches input color
|
||||||
fav_name.append(person) # append output list
|
# 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.
|
"""Returns a dict showing how many people like each color.
|
||||||
Any color liked by any of the people will be included, and only colors
|
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.
|
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,
|
"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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -9,12 +9,12 @@
|
|||||||
from unittest import TestCase, main
|
from unittest import TestCase, main
|
||||||
from people import friends, family
|
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,
|
||||||
)
|
)
|
||||||
|
|
||||||
# class TestCountPeople(TestCase):
|
# class TestCountPeople(TestCase):
|
||||||
@@ -49,42 +49,42 @@ from friend_functions import (
|
|||||||
# 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):
|
||||||
self.assertEqual(count_people_who_like_color(friends, "grey"), 2)
|
# 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, "beige"), 1)
|
||||||
self.assertEqual(count_people_who_like_color(friends, "cornflower"), 0)
|
# self.assertEqual(count_people_who_like_color(friends, "cornflower"), 0)
|
||||||
|
|
||||||
# class TestGetColorDict(TestCase):
|
class TestGetColorDict(TestCase):
|
||||||
# def test_returns_correct_dict(self):
|
def test_returns_correct_dict(self):
|
||||||
# familyExpected = {
|
familyExpected = {
|
||||||
# "aqua": 2,
|
"aqua": 2,
|
||||||
# "red": 2,
|
"red": 2,
|
||||||
# "blue": 2,
|
"blue": 2,
|
||||||
# "black": 2,
|
"black": 2,
|
||||||
# "white": 1,
|
"white": 1,
|
||||||
# "grey": 1,
|
"grey": 1,
|
||||||
# "yellow": 2,
|
"yellow": 2,
|
||||||
# "orange": 1,
|
"orange": 1,
|
||||||
# }
|
}
|
||||||
# friendsExpected = {
|
friendsExpected = {
|
||||||
# "lime": 1,
|
"lime": 1,
|
||||||
# "grey": 2,
|
"grey": 2,
|
||||||
# "sea foam": 1,
|
"sea foam": 1,
|
||||||
# "aqua": 2,
|
"aqua": 2,
|
||||||
# "teal": 1,
|
"teal": 1,
|
||||||
# "white": 1,
|
"white": 1,
|
||||||
# "red": 2,
|
"red": 2,
|
||||||
# "black": 2,
|
"black": 2,
|
||||||
# "blue": 2,
|
"blue": 2,
|
||||||
# "tan": 1,
|
"tan": 1,
|
||||||
# "sand": 1,
|
"sand": 1,
|
||||||
# "green": 1,
|
"green": 1,
|
||||||
# "beige": 1,
|
"beige": 1,
|
||||||
# "turquoise": 1,
|
"turquoise": 1,
|
||||||
# "yellow": 2,
|
"yellow": 2,
|
||||||
# }
|
}
|
||||||
# self.assertEqual(get_color_dict(family), familyExpected)
|
self.assertEqual(get_color_dict(family), familyExpected)
|
||||||
# self.assertEqual(get_color_dict(friends), friendsExpected)
|
self.assertEqual(get_color_dict(friends), friendsExpected)
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user