Question
Classes are a useful way of storing related data items (in much the same way that a dictionary can store key-value pairs). The code in
Classes are a useful way of storing related data items (in much the same way that a dictionary can store key-value pairs). The code in this exercise implements a class for holding name and email address information. The functionextract_namesoperates on a list ofContactobjects to extract the names and return them insortedorder.
Hint:Iterate through objects in theaddress_bookand append thenamemember variable associated with each object to a running list of names. Don't forget to sort the names before returning from the function.
Code:
class Contact:
"""Encapsulates an email contact."""
def __init__(self, name, email):
self.name = name
self.email = email
# DO NOT MODIFY CODE ABOVE THIS LINE
def extract_names(address_book):
"""Return a sorted list of all names in an address book,
implemented as a list of Contact objects."""
# TODO: implement this function and replace the return statement
return None
# DO NOT MODIFY CODE BELOW THIS LINE
addr_book = []
addr_book.append(Contact("Mickey Mouse", "..y@disneyland.com"))
addr_book.append(Contact("Minnie Mouse", "..e@disneyland.com"))
addr_book.append(Contact("Goofy", "..y@disneyland.com"))
addr_book.append(Contact("Pluto", "..o@disneyland.com"))
addr_book.append(Contact("Winnie the Pooh", "..r@disneyland.com"))
print(" ".join(extract_names(addr_book)))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started