Python one-letter-off word solver help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 2
Reputation: simonches is an unknown quantity at this point 
Solved Threads: 0
simonches simonches is offline Offline
Newbie Poster

Python one-letter-off word solver help

 
0
  #1
Sep 21st, 2009
Hi,
I'm trying to make a program in python that solves word puzzles like this:
"nj" (the answer to that is "ok")
where each letter is off by one, for example a or c instead of b.

the program is supposed to generate a list of possible words which will be checked against a dictionary.

it's recursive, and each time it's called it's supposed to find the two possible letters for the beginning letter and call itself on the word minus what's just been solved.

  1. def one(s):
  2. print "s:"+s
  3. if s == '':
  4. return [s]
  5. else:
  6. ans = []
  7. if s[1:] == '': return ans
  8. print "s1:"+s[1:]
  9. for o in one(s[1:]):
  10. ans.append(prevLetter(s[0])+string.join(one(s[1:]),''))
  11. ans.append(nextLetter(s[0])+string.join(one(s[1:]),''))
  12. print "o:"+o
  13. return ans

but i just can't figure out how to make it work.
can anybody help me?

by the way, the functions nextLetter and prevLetter return the next and previous letter, respectively, for example nextLetter('b') will return 'c'

Thanks,
Simon Chester
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Python one-letter-off word solver help

 
0
  #2
Sep 21st, 2009
Originally Posted by simonches View Post
but i just can't figure out how to make it work.
can anybody help me?
You were very close:
  1. def one(s):
  2. if s == '':
  3. return [s]
  4. else:
  5. ans = []
  6. if s[1:] == '': return ans
  7. for o in one(s[1:]):
  8. ans.append(prevLetter(s[0])+o)
  9. ans.append(nextLetter(s[0])+o)
  10. return ans
You never made use of o in your function. You were for whatever reason trying to use ''.join(x) (NOTE: that is equivalent to string.join(x, '') ).

Your function can be further simplified as such:
  1. def one4(s):
  2. if s == '': return [s]
  3. else:
  4. return [prevLetter(s[0]) + o for o in one(s[1:])] + \
  5. [nextLetter(s[0]) + o for o in one(s[1:])]
Last edited by jlm699; Sep 21st, 2009 at 12:53 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2
Reputation: simonches is an unknown quantity at this point 
Solved Threads: 0
simonches simonches is offline Offline
Newbie Poster

Re: Python one-letter-off word solver help

 
0
  #3
Sep 22nd, 2009
Thanks so much, it works perfectly!
Reply With Quote Quick reply to this message  
Reply

Tags
python, recursive, wordgame

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 777 | Replies: 2
Thread Tools Search this Thread



Tag cloud for python, recursive, wordgame
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC