Question
python

The Soundex Code code for surnames. It was code. All Al Soundex is a system that designed to produce one code for natha may b

Hi I have a project due on this Friday and I really can’t seem to figure this code out. If anyone could write this for me it’d be very appreciated. Thank you!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The required python code along with comments have been provided below:

def Soundex(surname):

   # Dictionary to story key:value pairs such that value is the
   # soundex code value of the key letter
   soundexDict = {}
   for letter in "BPFV":
       soundexDict[letter] = 1
   for letter in "CSKGJQXZ":
       soundexDict[letter] = 2
   for letter in "DT":
       soundexDict[letter] = 3
   for letter in "L":
       soundexDict[letter] = 4
   for letter in "MN":
       soundexDict[letter] = 5
   for letter in "R":
       soundexDict[letter] = 6

   # Final codename
   soundexCodename = ""
   # Converting everything to uppercase
   name = surname.upper()
   # Appending the first letter of the name to the codename
   soundexCodename += name[0]

   # List to store characters to be ignored
   ignoreCharacters = ['A','E','I','O','U','W','Y','H']

   # If the first letter was not in the ignored list
   if name[0] not in ignoreCharacters:
       # Store that the last adjacent letter's code number to prevent adding
       # an adjacent letter code with the same soundex code value
       lastAdjacentletterCode = soundexDict[name[0]]
   # Else, if the first letter was to be ignored
   else:
       # Simply store the last adjacent letter's code number as an invalid
       # soundex code value E.g = -1 or -2 or something
       lastAdjacentletterCode = -1

   # For every letter in the name after the first letter,
   for letter in name[1:]:

       # If the length of the codename has reached 4, then break out of
       # the for loop
       if len(soundexCodename) == 4:
           break
       # Else if the letter is not to be ignored
       elif letter not in ignoreCharacters:
           # If the last adjacent letter's soundex codenumber is not the same as
           # this letter code number,
           if soundexDict[letter] != lastAdjacentletterCode:
               # Append the letter to the codename
               soundexCodename += str(soundexDict[letter])
               # Update the last adjacent letter's code number to that of the
               # current letter
               lastAdjacentletterCode = soundexDict[letter]
       # Else if the letter was to be ignored, there is a break in adjacency, hence....
       else:
           # Make last adjacent letter's codenumber to some invalid value to show that
           # the adjacency is broken
           lastAdjacentletterCode = -1

   # After iterating through the name, if the soundexCode isn't 4 character long,
   if len(soundexCodename) < 4:
       # Format it to be appended upto length 4 with 0's
       soundexCodename = "{:0<4}".format(soundexCodename)

   # Return the code name
   return soundexCodename

if __name__ == "__main__":
   surname = raw_input("Enter surname: ")
   print("The soundex code is: "+Soundex(surname))

A screenshot is provided below for better understanding of the indentation etc.:

def Soundex(surname): # Dictionary to story key: value pairs such that value is the # soundex code value of the key letter so
if len(soundexCodename) break 4: # Else if the letter is not to be ignored elif letter not in ignoreCharacters: # If the last

Sample Input/Output:

Enter surname: Hammond
The soundex code is: H553

Enter surname: Partridge
The soundex code is: P636

Enter surname: Prater
The soundex code is: P636

Enter surname: Fischer
The soundex code is: F260

Enter surname: Lee
The soundex code is: L000

Enter surname: Lloyd
The soundex code is: L300

Thank you. Please upvote if you like my answer :)

Add a comment
Know the answer?
Add Answer to:
python Hi I have a project due on this Friday and I really can’t seem to figure this code out. If anyone could writ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT