#!/usr/bin/python # -*- encoding: utf-8 -*- __author__ = 'preuss' sudoku = ((0, 1, 2, 3, 4, 5, 6, 7, 8), # incorrect sudoku (0, 1, 2, 3, 4, 5, 6, 7, 8), (0, 1, 2, 3, 4, 5, 6, 7, 8), (0, 1, 2, 3, 4, 5, 6, 7, 8), (0, 1, 2, 3, 4, 5, 6, 7, 8), (0, 1, 2, 3, 4, 5, 6, 7, 8), (0, 1, 2, 3, 4, 5, 6, 7, 8), (0, 1, 2, 3, 4, 5, 6, 7, 8), (0, 1, 2, 3, 4, 5, 6, 7, 8), ) sudoku = ((8, 3, 5, 4, 1, 6, 0, 2, 7), # correct sudoku (2, 0, 6, 8, 5, 7, 4, 3, 1), (4, 1, 7, 2, 0, 3, 6, 5, 8), (5, 6, 0, 1, 3, 4, 7, 8, 2), (1, 2, 3, 6, 7, 8, 5, 4, 0), (7, 4, 8, 5, 2, 0, 1, 6, 3), (6, 5, 2, 7, 8, 1, 3, 0, 4), (0, 8, 1, 3, 4, 5, 2, 7, 6), (3, 7, 4, 0, 6, 2, 8, 1, 5)) def line_check(line): return set(line) == set(range(9)) # http://stackoverflow.com/questions/19339/a-transpose-unzip-function-in-python-inverse-of-zip def transpose_sudoku(sudoku): return zip(*sudoku) def block_sudoku(sudoku): ''' Da Sie noch keine List-Comprehension kennen: hier die Langversion: sp_sudoku = [] for x in 0,3,6: for y in 0,3,6: sp_sudoku.append([sudoku[x][y], sudoku[x+1][y], sudoku[x+2][y], sudoku[x][y+1], sudoku[x+1][y+1], sudoku[x+2][y+1], sudoku[x][y+2], sudoku[x+1][y+2], sudoku[x+2][y+2], ]) return sp_sudoku ''' return [[sudoku[x + i][y + j] for i in 0, 1, 2 for j in 0, 1, 2] for x in 0, 3, 6 for y in 0, 3, 6] def simple_check(s): for line in s: if not line_check(line): return False return True def complete_check(sudoku): return simple_check(sudoku) and simple_check(transpose_sudoku(sudoku)) and simple_check(block_sudoku(sudoku)) if __name__ == '__main__': print(complete_check(sudoku))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question