Skip to content

Instantly share code, notes, and snippets.

@manpages
Created January 7, 2015 21:04
Show Gist options
  • Save manpages/53994db8fc8cf5102774 to your computer and use it in GitHub Desktop.
Save manpages/53994db8fc8cf5102774 to your computer and use it in GitHub Desktop.
dict wtf
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