Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Telephone numbers used to be encoded with letters so that it was easier to remem

ID: 3757250 • Letter: T

Question

Telephone numbers used to be encoded with letters so that it was easier to remember numbers. The encoding was 2: abc 3: def 4: ghi 5: jkl 6: mno 7: pqrs 8: tuv 9: wxyz 0: A telephone number such as 1-414-846-6270 would represent "Thomas " and phone numbers that represent names are called vanity numbers. Any name with 7 letters can be represented by one vanity number and names with less letters can be represented by more by interspersing with the numbers 0 and 1 especially at the beginning and end Write a function that given a lower-case letter return the corresponding digit (as a letter, not as an integer.) Right now, we have to do this with case distinctions, but soon, we will learn to do it more efficiently using dictionaries. Write a function that takes a seven-digit phone number (a string, NOT an integer) and returns a string without 0 and 1 digits Write a function that tests (returns a True or False) whether a vanity number can represent a name Use the file "first.txt" that contains first names from the US census bureau. The first word on each line is a first name. Use this text file to implement a function that prints out all names that could have a given phone number as a vanity number For example, for "3284310", your function should return "david".

Explanation / Answer

# file must be saved as namefile.txt in the same directory as this python file.

# takes a letter and return a digit as a STRING.
# not used here but returns -1 of type integer if given anything
# else other than a lowercase english alphabet
def getDigit(l):
if(l=='a' or l=='b' or l=='c'):
return '2'
elif(l=='d' or l=='e' or l=='f'):
return '3'
elif(l=='g' or l=='h' or l=='i'):
return '4'
elif(l=='j' or l=='k' or l=='l'):
return '5'
elif(l=='m' or l=='n' or l=='o'):
return '6'
elif(l=='p' or l=='q' or l=='r' or l=='s'):
return '7'
elif(l=='t' or l=='u' or l=='v'):
return '8'
elif(l=='w' or l=='x' or l=='y' or l=='z'):
return '9'
else:
return -1;

# take a string of numbers as input and return after removing 'o' and '1' from it
def remove01(number):
newNumber=""
for n in list(number):
if (n!='0' and n!='1'):
newNumber=newNumber+n
return newNumber
# check if the given number is a vanity number for given name
def checkvanity(name,number):

# spliting the name into letters and getting digit for every letter
# thus coverting the given name into a vanity number
letters=list(name)
tempnumb1=""
for letter in letters:
tempnumb1=tempnumb1+getDigit(letter)

# removing '0' and '1' from the given number
tempnumb2=remove01(number)

# checking if the the converted name and the given number are equal or not.
if (tempnumb2==tempnumb1):
return True
return False

# getting names from file and checking every name against the given number.
# file must be saved as namefile.txt in the same directory as this python file.
def vanityfromfile(number):
validnames=[]
with open("namefile.txt") as f:
for line in f:
# print(line.strip().split(" ")[0])
name = line.strip().split(" ")[0].lower()
if (checkvanity(name,number)):
validnames.append(name)
return validnames
# calling the driver function against "3284310" should return david from the file.
foundnames=vanityfromfile('3284310')
for name in foundnames:
print(name)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote