Jump to content

Python Help!

X1XNobleX1X

Hello forums,

I'm again in need of Python help. 

 

This is what the question is asking us to do.

Given two words with the same number of letters in each, work out how many letters need to be changed to get from the first word to the second. A more complex version of edit distance is commonly used in spelling auto-correct algorithms on phones and word processors to find candidate corrections.

The two words should be read from the user with one word per line. For example:

 
 
 
 
 
Word 1: hello
Word 2: jelly
2
 

That's because h needs to be changed to j and o needs to be changed to y.

 

This is my coding at the moment:

 

word1 = input("Word 1:")

word2 = input("Word 2:")
word = 0
for word in range(word1,word2):
  print(word)
word == word + 1
 
Please help!

|CPU: Intel 5960X|MOBO:Rampage V Extreme|GPU:EVGA 980Ti SC 2 - Way SLI|RAM:G-Skill 32GB|CASE:900D|PSU:CorsairAX1200i|DISPLAY :Dell U2412M X3|SSD Intel 750 400GB, 2X Samsung 850 Pro|

Peripherals : | MOUSE : Logitech G602 | KEYBOARD: K70 RGB (Cherry MX Brown) | NAS: Synology DS1515+  - WD RED 3TB X 5|ROUTER: AC68U

Sound : | HEADPHONES: Sennheiser HD800 SPEAKERS: B&W CM9 (Front floorstanding) ,  B&W CM Center 2 (Centre) | AV RECEIVER : Denon 3806 | MY X99 BUILD LOG!

 

Link to comment
Share on other sites

Link to post
Share on other sites

You will need to enumerate both words and iterate over the characters, comparing them to each other. So something like:

    changes = 0    for char in word1:        for char2 in word2:            if char != char2:                changes = changes + 1

Now this is a very naive implementation and does not take care of differences in length of the words etc. but this might get you somewhere...

 

Basically what you are looking for here is a Levenshtein distance algorithm, loads of resources about that on the wide web.

Link to comment
Share on other sites

Link to post
Share on other sites

You will need to enumerate both words and iterate over the characters, comparing them to each other. So something like:

    changes = 0    for char in word1:        for char2 in word2:            if char != char2:                changes = changes + 1

Now this is a very naive implementation and does not take care of differences in length of the words etc. but this might get you somewhere...

 

Basically what you are looking for here is a Levenshtein distance algorithm, loads of resources about that on the wide web.

Well, I'v got this, not sure what to do next. Any tips :)

word1 = input("Word 1: ")
word2 = input("Word 2: ")
changes = 0
for char in word1:
 for char2 in word2:
   if char != char2:
        changes == changes + 1
print(changes)

|CPU: Intel 5960X|MOBO:Rampage V Extreme|GPU:EVGA 980Ti SC 2 - Way SLI|RAM:G-Skill 32GB|CASE:900D|PSU:CorsairAX1200i|DISPLAY :Dell U2412M X3|SSD Intel 750 400GB, 2X Samsung 850 Pro|

Peripherals : | MOUSE : Logitech G602 | KEYBOARD: K70 RGB (Cherry MX Brown) | NAS: Synology DS1515+  - WD RED 3TB X 5|ROUTER: AC68U

Sound : | HEADPHONES: Sennheiser HD800 SPEAKERS: B&W CM9 (Front floorstanding) ,  B&W CM Center 2 (Centre) | AV RECEIVER : Denon 3806 | MY X99 BUILD LOG!

 

Link to comment
Share on other sites

Link to post
Share on other sites

Your next step is to research what the algorithm I have mentioned does. Use your favorite search engine :)

Link to comment
Share on other sites

Link to post
Share on other sites

 

This is what I've come up with:

#get inputword1 = raw_input("Enter the first word: ");word2 = raw_input("Enter the second word: ");#get length of wordslength1 = len(word1)length2 = len(word2)#print "Your first word is %s and it's %s chars long." % (word1, length1);#print "Your second word is %s and it's %s chars long." % (word2, length2);if length1 != length2:    print "Those two words aren't the same length!";else:    diff = 0    count = 0    for char in word1:        if char != word2[count]:            diff = diff + 1;            count = count + 1        else:            count = count + 1    if diff == 1:        print "There is %s difference between %s and %s." % (diff, word1, word2);    else:        print "There are %s differences between %s and %s." % (diff, word1, word2);

"Everybody wants a happy ending, right? But it doesn’t always roll that way." - TS

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×