Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions