-1

Soundex is a phonetic algorithm that encodes a word into a letter followed by three numbers that roughly describe how the word sounds. Similar sounding words have the same four-character codes. For instance, the words Carrot and Caret are both coded as C123. A slight variation of the Soundex coding algorithm is as follows:

  • Retain the first letter.
  • For the remaining letters, delete all occurrences of a, e, i, o, u, h, y, and w.
  • Assign numbers to the other letters that remain so that
    • b, f, p, and v → 1
    • c, g, j, k, 9, s, x, and z → 2
    • d and t both → 3
    • l ('el') → 4
    • m and n → 5
    • r → 6
  • If two or more letters that have been replaced by the same number were next to each other in the original full word, keep only the first of them.
  • Keep only the first four characters of what you have left. If you have fewer than four, then add zeros on the end to make the string have length four. Write a program that carries out the algorithm.

Sample output:

Enter a word to code: Robert
The coded word is R163.

Code:

def soundexCode(string):
    result = string[0] +""
    for i in range(1, len(string)):
        if(string[i] == 'b' or string[i] == 'f' or string[i] == 'p' or
           string[i] == 'v'):
            if(result[-1]!= '1'):
                result = result + '1';

        elif (string[i] == 'c' or string[i] == 'g' or string[i] == 'j' or
              string[i] == 'k' or string[i] == 'q' or string[i] == 's' or
              string[i] == 'x' or string[i] == 'z'):
            if (result[-1] != '2'):
                result = result +'2';

        elif string[i] == 'd' or string[i] == 't':
            if (result[-1] != '3'):
                result = result + '3';

        elif string[i] == 'l' :
            if (result[-1] != '4'):
                result = result + '4';

        elif string[i] == 'm' or string[i]== 'n' :
            if(result[-1] != '5'):
                result = result + '5';

        elif string[i] == r :
            if result[-1] != '6':
                result = result + '6';

    return result

    word = input ('Enter a word to code : ')
    print("\nThe coded word is " + soundexCode(word))
martineau
  • 119,623
  • 25
  • 170
  • 301
cupof
  • 7
  • 3
  • 2
    What is your question, exactly? – martineau Jun 05 '21 at 16:08
  • I write this code but it doesnt work. It doesnt ask enter any word. when I run the code just says "Press any key to continue" – cupof Jun 05 '21 at 17:16
  • At a bare minimum, it should prompt you for the word and allow you to type it in. How are you running the script? – martineau Jun 05 '21 at 17:22
  • I wrote on just like I was written here. I will add a photo in my question. I tried on visual studio but I couldnt do it again – cupof Jun 05 '21 at 17:46
  • Oh, so you've got the last two lines indented making them are part of the function (not like what's currently posted in your question). That's why nothing happens, all you're doing is _defining_ `soundexCode()` function but never executing it. Unindent those two lines one level and try it again. – martineau Jun 05 '21 at 18:07
  • thank you so much. I'm a bit of a novice. thanks for the edit and help – cupof Jun 05 '21 at 19:27
  • You're welcome — we all gotta start somewhere. In Python indentation is _very_ important (unlike in many other languages). This makes getting it right crucial to the proper execution of your code. – martineau Jun 05 '21 at 19:39
  • yes really it is interesting. I want to ask one more thing. For the last item what should i do? I mean adding zeros. How should I do? – cupof Jun 05 '21 at 20:31
  • _Before_ the `return` you could add an `if len(result) < 4: result.extend([0] * (4-len(result)))`. – martineau Jun 05 '21 at 21:47

1 Answers1

1

Missing quotes around the string 'r' in line 24:

    elif string[i] == 'r' :
RustyB
  • 147
  • 1
  • 11
  • oh thank you but it still doesn't work. When I run the code, I cant enter any word. – cupof Jun 05 '21 at 15:52
  • Do you get an error message? If so, what is it? – BoarGules Jun 05 '21 at 16:01
  • no it run but when it runs says "Press any key to continue" – cupof Jun 05 '21 at 16:31
  • Must be an environment issue. My implementation returns exactly what you expected above. – RustyB Jun 06 '21 at 06:43
  • 1
    @RustyB: Your answer's partially correct. Turns out the cause of the function not running was an indentation issue which was inadvertently obfuscated by yours truly trying to improve the question — changes that I have now (partially) undone to make things clearer and more comprehensible to future readers. – martineau Jun 06 '21 at 12:30