(PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON)
ID: 3856616 • Letter: #
Question
(PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON) (PYTHON)
Write a function called area_code, with three parameters: the first, a dictionary, the second, a string (last name), and the third a string (first name). Assume that dictionary entries are of the form (lastname, firstname) : "pho-nen-umbr". For example, {("Smith", "Bob") : "400-123-1234", ("Rogers", "Alberto") : "561-125-3510"}. Your function should return the area code of the phone number corresponding to the given first and last name. If there is no person with that name in the phonebook, return -1.
Write a function called area code, with three parameters: the first, a dictionary, the second, a string (last name), and the third a string (first name). Assume that dictionary entries are of the form (lastname, firstname): "pho-nen-umbr". For example, f("Smith", "Bob"): "400-123-1234", ("Rogers", "Alberto"): "561-125-3510". Your function should return the area code of the phone number corresponding to the given first and last name. If there is no person with that name in the phonebook, return -1. For example: Test Result print (area_code(ft Smith", "Bob")"400-123-1234", ("RogersAlberto"561-125- 400 510", "Smith", Bob) print (area_code "Smith", "Bob"): "400-123-1234", "Rogers", "Alberto) : "561-125- 510","Souza", "JP")) 1Explanation / Answer
def area_code(dictionary,lname,fname):
'''Logic is we will go through each key of dictionary and convert the tuple of(lanme,fname) into a single string and compare this string with existing inputed lanme,fname params.'''
found = 0
input_full_name=((lname+' '+fname).lower()).strip() #Making inputed lname,fname to a string
for key in dictionary: #Go through every key(lanme,fname) of dictionary.
full_name=''
for values in key:
full_name=full_name+' '+values #Make a string with lname+fname
full_name=full_name.lower().strip() #Strip it to remove unnecessary spaces
phone_number=dictionary[key]
if(full_name == input_full_name): #Check whether inputed lname+fname is equals to formed lname+fname of this iteration
found = 1
postal_code=phone_number.split('-')[0] #if equal split the phone number to get postal_code
return postal_code
if(found == 0):
return -1
print(area_code( {("Smith", "Bob") : "400-123-1234", ("Rogers", "Alberto") : "561-125-3510"},"Smith", "Bob"))
print(area_code( {("Smith", "Bob") : "400-123-1234", ("Rogers", "Alberto") : "561-125-3510"},"Souza", "JP"))
Note : Please remove the comments if you are unable to execute this code.Sometimes comments may cause indentation error.
For executing purpouse im posting code without comments down here. area_code.py
def area_code(dictionary,lname,fname):
found = 0
input_full_name=((lname+' '+fname).lower()).strip()
for key in dictionary:
full_name=''
for values in key:
full_name=full_name+' '+values
full_name=full_name.lower().strip()
phone_number=dictionary[key]
if(full_name == input_full_name):
found = 1
postal_code=phone_number.split('-')[0]
return postal_code
if(found == 0):
return -1
print(area_code( {("Smith", "Bob") : "400-123-1234", ("Rogers", "Alberto") : "561-125-3510"},"Smith", "Bob"))
print(area_code( {("Smith", "Bob") : "400-123-1234", ("Rogers", "Alberto") : "561-125-3510"},"Souza", "JP"))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.