Created
January 7, 2015 21:04
-
-
Save manpages/53994db8fc8cf5102774 to your computer and use it in GitHub Desktop.
dict wtf
This file contains hidden or 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
from pprint import pprint | |
def parseField(input): | |
return dict( { 'cells': parseCells(input), | |
'cages': parseCages(input) }) | |
def parseCells(input): | |
solutionMaybe = solution(input) | |
if solutionMaybe: | |
return solutionMaybe | |
else: | |
latinSquare(getWidth(input)) | |
def solution(input): | |
inputList = input.split('\n\n') | |
if len(inputList) >= 3: | |
return solutionDo(input[2].split('\n'), | |
0, | |
{}) | |
def solutionDo(solutionBlock, y, acc): | |
if len(solutionBlock) == 0: | |
return acc | |
head = solutionBlock[0] | |
tail = solutionBlock[1:] | |
for x in range(0, len(head)): | |
acc[(x,y)] = head[x] | |
solutionDo(tail, y + 1, acc) | |
def latinSquare(n): | |
ls = latinSquareDo(n, range(1, n+1), {}) | |
return ls | |
def latinSquareDo(n, sample, acc): | |
if n == 0: | |
pprint(acc) | |
return acc | |
for x in range(0, len(sample)): | |
acc[(x,n-1)] = sample[x] | |
latinSquareDo(n-1, shiftDo(sample), acc) | |
def shiftDo(xs): | |
return xs[1:] + [xs[0]] | |
if __name__ == '__main__': | |
pprint(latinSquare(3)) | |
#################################################################### | |
# OUTPUT | |
#################################################################### | |
# Wed Jan 07 09:59:48:043113682 sweater@brainstorm ~/github/py-ken | |
# λ python dictwtf.py | |
# {(0, 0): 3, | |
# (0, 1): 2, | |
# (0, 2): 1, | |
# (1, 0): 1, | |
# (1, 1): 3, | |
# (1, 2): 2, | |
# (2, 0): 2, | |
# (2, 1): 1, | |
# (2, 2): 3} | |
# None | |
#################################################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment