943,695 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1834
  • Python RSS
Sep 21st, 2009
0

Python one-letter-off word solver help

Expand Post »
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.

python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simonches is offline Offline
2 posts
since Jul 2008
Sep 21st, 2009
0

Re: Python one-letter-off word solver help

Click to Expand / Collapse  Quote originally posted by simonches ...
but i just can't figure out how to make it work.
can anybody help me?
You were very close:
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Sep 22nd, 2009
0

Re: Python one-letter-off word solver help

Thanks so much, it works perfectly!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simonches is offline Offline
2 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Help with server side (cgi) python scripting
Next Thread in Python Forum Timeline: Python Serial Output Works





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC