Last active
February 7, 2017 09:49
-
-
Save jashsu/3f5eaf0398f49c8fa68431febde78847 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> def findchildren(chain, tail, used): | |
for move in bogmoves: | |
newtail = tuple(map(operator.add, tail, move)) | |
if newtail[0] > -1 and newtail[0] < bogsize and\ | |
newtail[1] > -1 and newtail[1] < bogsize and\ | |
newtail not in used: | |
#sys.stdout.write(">") | |
#if using isValid() then insert check here | |
bogchains.append(findchildren(chain + bogboard[newtail[0]][newtail[1]], newtail, used + [newtail])) | |
#sys.stdout.write("<") | |
return chain | |
>>> def generatelist(): | |
for x in xrange(bogsize): | |
for y in xrange(bogsize): | |
head = (x,y) | |
print head | |
bogchains.append(findchildren(bogboard[x][y],head,[head])) | |
#print "\n" + str(bogchains) | |
>>> bogboard = [['a','t','e','e'],['a','p','y','o'],['t','i','n','u'],['e','d','s','e']] | |
>>> bogsize = 4 | |
>>> bogmoves = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] | |
>>> generatelist() | |
(0, 0) | |
(0, 1) | |
(0, 2) | |
(0, 3) | |
(1, 0) | |
(1, 1) | |
(1, 2) | |
(1, 3) | |
(2, 0) | |
(2, 1) | |
(2, 2) | |
(2, 3) | |
(3, 0) | |
(3, 1) | |
(3, 2) | |
(3, 3) | |
>>> len(bogchains) | |
12029704 | |
>>> 'tepee' in bogchains | |
False | |
>>> 'pins' in bogchains | |
True | |
>>> 'pines' in bogchains | |
True | |
>>> 'dates' in bogchains | |
False | |
>>> 'pint' in bogchains | |
False | |
>>> 'sid' in bogchains | |
True | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://coursera.cs.princeton.edu/algs4/assignments/boggle.html