Nonograms

Initial, probably buggy, load of a literate python program.

Comments are moderated. It may take a few minutes before your comment appears.
Markdown is supported in your comments.

def solution(rows, cols):
    "Converts a list of clues into the puzzle data structure."
    populate_index(rows, cols)
    # ( (name, row_clue, length), ... )
    rows2 = izip(names('R', len(rows)), rows, repeat(len(cols)))
    cols2 = izip(names('C', len(cols)), cols, repeat(len(rows)))
    # [ (row_count, name, row_clue, length), ... ]
    rows3 = iter(sorted((row_count(c,l), n, c, l) for n,c,l in rows2))
    cols3 = iter(sorted((row_count(c,l), n, c, l) for n,c,l in cols2))
    # [ (name, row_clue, length), ... ]
    puzzle = [datum[1:] for datum in interleave(rows3, cols3)]
    board = {}
    solve(puzzle, board)

def test():
    rows = [[3], [2,2], [2,2], [1,1], [2,2], [2,2], [3]]
    cols = [[3], [2,2], [2,2], [1,1], [2,2], [2,2], [3]]
    solution(rows, cols)

def prof():
    import cProfile
    cProfile.run('test()', sort=1)

if __name__ == "__main__":
    test()
    #prof()
Name:
Mail: (not shown)

Please type this: