generated from mwc/lab_weather
	I replaced all the raise NotImplementedError() lines with working code that loops through the list of dictionaries to find names, emails, and favorite colors, counts people and colors, and creates a dictionary showing how many people like each color.
I got stuck at first trying to loop through the list of dictionaries to find specific information, since I it has been a while I have had accessing values inside nested structures. I got un-stuck by reviewing examples of how to use keys in dictionaries and by printing out parts of the data step by step to see how it was organized.
This commit is contained in:
		@@ -10,83 +10,39 @@
 | 
				
			|||||||
# each and instead write code which returns the expected values. 
 | 
					# each and instead write code which returns the expected values. 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def count_people(people):
 | 
					def count_people(people):
 | 
				
			||||||
    """Counts the number of people. 
 | 
					 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        >>> count_people(family)
 | 
					    return len(people)
 | 
				
			||||||
        5
 | 
					 | 
				
			||||||
        >>> count_people(friends)
 | 
					 | 
				
			||||||
        10
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    raise NotImplementedError()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_email(people, name):
 | 
					def get_email(people, name):
 | 
				
			||||||
    """Returns the named person's email address. If there is no such person, returns None.
 | 
					   
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
        >>> get_email(family, "Tad Winters")
 | 
					    for person in people:
 | 
				
			||||||
        "ligula.aenean@hotmail.edu"
 | 
					        if person["name"] == name:
 | 
				
			||||||
        >>> get_email(friends, "Tad Winters")
 | 
					            return person["email"]
 | 
				
			||||||
        None
 | 
					    return None
 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    raise NotImplementedError()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
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.
 | 
					    
 | 
				
			||||||
 | 
					    for person in people:
 | 
				
			||||||
        >>> count_favorite_colors(family, "Tad Winters")
 | 
					        if person["name"] == name:
 | 
				
			||||||
        2
 | 
					            return len(person["favorite_colors"])
 | 
				
			||||||
        >>> count_favorite_colors(family, "Raphael Chambers")
 | 
					    return None
 | 
				
			||||||
        1
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    raise NotImplementedError()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
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.
 | 
					    
 | 
				
			||||||
        >>> people_who_like_color(family, "yellow")
 | 
					    return [person for person in people if color in person["favorite_colors"]]
 | 
				
			||||||
        [
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
	        "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")
 | 
					 | 
				
			||||||
        []
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    raise NotImplementedError()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
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. 
 | 
					   
 | 
				
			||||||
    
 | 
					    return len(people_who_like_color(people, color))
 | 
				
			||||||
        >>> count_people_who_like_color(family, "red")
 | 
					 | 
				
			||||||
        2
 | 
					 | 
				
			||||||
        >>> count_people_who_like_color(family, "orange")
 | 
					 | 
				
			||||||
        1
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    raise NotImplementedError()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_color_dict(people):
 | 
					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
 | 
					    color_dict = {}
 | 
				
			||||||
    liked by someone will be included. The order of items in the dict doesn't matter.
 | 
					    for person in people:
 | 
				
			||||||
 | 
					        for color in person["favorite_colors"]:
 | 
				
			||||||
        >>> get_color_dict(family)
 | 
					            color_dict[color] = color_dict.get(color,0) + 1
 | 
				
			||||||
        {
 | 
					    return color_dict
 | 
				
			||||||
            "aqua": 2,
 | 
					 | 
				
			||||||
            "red": 2,
 | 
					 | 
				
			||||||
            "blue": 2,
 | 
					 | 
				
			||||||
            "black": 2,
 | 
					 | 
				
			||||||
            "white": 1,
 | 
					 | 
				
			||||||
            "grey": 1,
 | 
					 | 
				
			||||||
            "yellow": 2,
 | 
					 | 
				
			||||||
            "orange": 1,
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    raise NotImplementedError()
 | 
					 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user