| | |
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:
Solved Threads: 0
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.
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
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)
def one(s): print "s:"+s if s == '': return [s] else: ans = [] if s[1:] == '': return ans print "s1:"+s[1:] for o in one(s[1:]): ans.append(prevLetter(s[0])+string.join(one(s[1:]),'')) ans.append(nextLetter(s[0])+string.join(one(s[1:]),'')) print "o:"+o 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
•
•
•
•
but i just can't figure out how to make it work.
can anybody help me?
python Syntax (Toggle Plain Text)
def one(s): if s == '': return [s] else: ans = [] if s[1:] == '': return ans for o in one(s[1:]): ans.append(prevLetter(s[0])+o) ans.append(nextLetter(s[0])+o) return ans
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)
def one4(s): if s == '': return [s] else: return [prevLetter(s[0]) + o for o in one(s[1:])] + \ [nextLetter(s[0]) + o for o in one(s[1:])]
Last edited by jlm699; Sep 21st, 2009 at 12:53 pm.
![]() |
Similar Threads
- First letter of each word (Python)
- Capitalizing first letter of word in an array? (C)
- How do i change a letter out of a of a word (C++)
- Guess a word (Python)
- Word and Letter Count (C)
Other Threads in the Python Forum
- Previous Thread: Help with server side (cgi) python scripting
- Next Thread: Python Serial Output Works
Views: 777 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for python, recursive, wordgame
abrupt address advanced advice aliased apax avogadro beginner binary c++ calculator client code console copy corners cturtle curves data def development embed enter event examples excel file function generator gui images input itunes jaunty java launcher linux list lists maze method microsoft module mouse movingimageswithpygame mysqldb newb opensource parameters path prime print programming projects push py2exe pygame pyglet pyqt python rails random raw_input read recursion recursive return reverse ruby rubyconf shebang silverlight simple smtp socket source ssh string strings strip sum table tennis text threading tkinter tree tutorial ubuntu url urllib urllib2 variable volume web-scrape wikipedia wordgame write wxpython xlwt







