lab_weather/friend_functions.py

87 lines
2.4 KiB
Python

# friend_functions.py
# ------------
# By MWC Contributors
#
# Each of the functions below expects a list of dictionaries as its first
# argument. Two examples of the expected input are provided in people.family
# and people.friends.
# Your job is to complete these functions. Remove the NotImplementedError from
# each and instead write code which returns the expected values.
from people import friends, family
def count_people(people):
"""Counts the number of people.
>>> count_people(family)
5
>>> count_people(friends)
10
"""
counted = 0
for person in people:
counted = counted + 1
return counted
def get_email(people, name):
for person in people:
if person["name"] == name:
return person["email"]
return None
def count_favorite_colors(people, name):
color_count = 0
for person in people:
if person["name"] == name:
for colors in person["favorite_colors"]:
color_count = color_count + 1
return color_count
return None
def people_who_like_color(people, color):
people_list = []
for person in people:
for colors in person["favorite_colors"]:
if colors == color:
people_list.append(person)
return people_list
def count_people_who_like_color(people, color):
people_liking_color = people_who_like_color(people,color)
people_count = 0
for person in people_liking_color:
people_count = people_count + 1
return people_count
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.
>>> get_color_dict(family)
{
"aqua": 2,
"red": 2,
"blue": 2,
"black": 2,
"white": 1,
"grey": 1,
"yellow": 2,
"orange": 1,
}
"""
# First, get a dictionary of all the colors that are liked.
color_dict = {}
for person in people:
for colors in person["favorite_colors"]:
color_dict[colors] = "0"
# Then, assign the correct numbers to each color.
for key in color_dict:
color_dict[key] = count_people_who_like_color(people,key)
return color_dict