HW 3 Sudoku

This has been graded as incorrect. Could anyone tell me what is wrong with this answer? (If posting this code is not appropriate I will delete it). Thank you!

#THREE GOLD STARS

#Sudoku [http://en.wikipedia.org/wiki/Sudoku]
#is a logic puzzle where a game
#is defined by a partially filled
#9 x 9 square of digits where each square
#contains one of the digits 1,2,3,4,5,6,7,8,9.
#For this question we will generalize
#and simplify the game.

#Define a procedure, check_sudoku,
#that takes as input a square list
#of lists representing an n x n
#sudoku puzzle solution and returns
#True if the input is a valid
#sudoku square and returns False
#otherwise.

#A valid sudoku square satisfies these
#two properties:

#   1. Each column of the square contains
#       each of the numbers from 1 to n exactly once.

#   2. Each row of the square contains each
#       of the numbers from 1 to n exactly once.

correct = [[1,2,3],
           [2,3,1],
           [3,1,2]]

incorrect = [[1,2,3,4],
             [2,4,1,3],
             [4,1,2,3],
             [1,2,3,4]]

def check_sudoku(list):
    row = []
    column = []
    counter = 0
    while counter < len(list):
        for x in list:
            if x[counter] in column:
                return False
            else:
                column.append(x[counter])
        column = []
        counter = counter + 1

    for x in list:
        for y in x:
            if y in row:
                return False
            else:
                row.append(y)
        row = []
    return True

print check_sudoku(correct)
print check_sudoku(incorrect)

asked 14 Mar '12, 19:27

Tadeusz%20Jordan's gravatar image

Tadeusz Jordan
1481118
accept rate: 85%


3 Answers:

No, it was correctly graded as wrong! It fails those tests:
incorrect3 = [[1,2,3],[4,5,6],[7,8,9]]
incorrect35 = [[0,2,3],[2,3,1],[3,1,2]]

You did not validate the minimum/maximum value.

And it is not an invalid post! :)

link

answered 14 Mar '12, 20:09

Angel's gravatar image

Angel
7.0k632112

edited 14 Mar '12, 20:10

I did the same thing :/

Thanks to angel for pointing it out.

link

answered 14 Mar '12, 23:05

Douglas%20Finnigan's gravatar image

Douglas Finn...
146139

ahh I forgot to check if the numbers are out of range :) Thanks!

link

answered 14 Mar '12, 20:11

Tadeusz%20Jordan's gravatar image

Tadeusz Jordan
1481118

Your answer
Question text:

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,330
×195
×13

Asked: 14 Mar '12, 19:27

Seen: 231 times

Last updated: 14 Mar '12, 23:05