Critique my HW 3.8 Sudoku solution

I see that Peter goes through each column and row for every single digit. My idea is to take every row and every column as a separate list, sort them, and check if the digits go from 1 to n. Please comment on the efficiency / correctness of my solution (it's judged correct by the system).

def check (ls):
    a = []
    a.extend(ls)
    a.sort()
    k = 1
    for l in a:
        if not l==k:
            return False
        k = k + 1
    return True

def check_sudoku(ls):
    n = len(ls)
    for l in ls:
        if not check(l):
            return False
    for i in range(n):
        a = []
        for j in range(n):
            a.append(ls[j][i])
        if not check(a):
            return False
    return True

asked 14 Mar '12, 18:42

Bo%20Tian's gravatar image

Bo Tian
498
accept rate: 0%


One Answer:

I guess I don't understand why you need to sort the lists in order to check them. I realize it's computationally insignificant for what we're doing, but you can just run a for loop over the range of the length of the list and check that the number is there.

link

answered 14 Mar '12, 18:47

David%20Harris's gravatar image

David Harris
8.1k2155111

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,242
×161

Asked: 14 Mar '12, 18:42

Seen: 136 times

Last updated: 14 Mar '12, 18:47