Python
program
Please find below the program screenshots in sequence:
OUTPUT
Run1
Enter surname:Fischer
Soundex code is: F260
Run2
Enter surname:Partridge
Soundex code is: P636
Run3
Enter surname:Lee
Soundex code is: L000
搟unction : generates°undexCode 3 #--> The function generates soundexcode for data 4 #by checking the consonant codes from dictData #The code is a srtring of Length 4 7- def generatesoundexCode (dictData, data): lastcode '. 8 finalcode for counter in range(0, n(data)) : #traverse each haracter of the data currentchar data[counter] #get the character currentCode0 9 12 it (counter-0): # for the first character of string fina!Code :-currentchar ; #1t will be the consonant in uppercase itself else: # for alt other characters in data in sequence currentcode getcode(dictData, currentchar) #ger code for the character if(currentcodel-lastcode): #if the code is not matched with the previous code finalCode finalCode + str (currentcode) # append the code to finalCode if(le (fina!Code) 4): #stop fina LCode generation if its Length is 4 15- 16 19 break # update Lastcode with currentcode; this will be used #to tackle adjacent letter with same code lastCodecurrentCode 21 23 #end for 25 #Left adjusr fina!Code by appending 0 to its left #if it is not of length 4 after the end of for loop finalcode finalcode. ljust(4, 'e) return fina!Code #return finalCode 31 #end def 32
34 #F unction : getCode 35 #--> get code for currentcharacter by consulting 36 #dictData dictionary 37 #will be called by generateSoundexCode() 39 def getCode (dictData, currentChar): finalcode-'' 40 #check for currentchar in values of the dictionary #if found, update fina LCode with the key of the value in which 42 43 #t is found and stop checking for key,value in dictData.items (): for currentValue in value: 45- if currentChar in currentValue: 46 finalcode key 47 48 rea 49 50 #end if 51 #end for fina!Code;#return code 52 return #end def 53 54
56 #program starts here 58 0 59 #the code for the consonants are tken into a dictionary 62 #user input for surname is captured into surName field 63 surName input ("Enter surname:") 65 #surName is converted to uppercase so that all codes can be checked for uppercases 66 surName: sur-Name . upper() 67 68 #the soundex code is then generated by calling generates°undexcode function 69 #codeDict dictionary and the user input surName is passed to the function 70 soundexCode generateSoundexcode(codeDict, surName) 71 72 print("Soundex code is: ", soundexCode) 74