faraz
faraz

Reputation: 2733

why doesnt this double loop work?

First of all, here's the code:

def check_sudoku(n):
    d=len(n)
    i=0
    s=1
    while i<d:
        print "i=",i
        while s<d:
            print "s=",s
            if n[0][i]==n[s][i]:
                return False
            s=s+1
            i=i+1
         return True 

what I want to do is that after the value of s changes from 1 to d, then it loops again and the value of i changes. But in my code the value of i is not changing at all.

Just to be clear of what I want to do, say

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

i want the following to happen:

after this value of i will increase again and this same loop will run.

This is not happening and I am not sure why. Please tell me what changes I should make to make it run the way I want to.

Upvotes: 1

Views: 137

Answers (4)

Martijn Pieters
Martijn Pieters

Reputation: 1122092

Your inner loop increments s until it is greater than d, but you never set it back to 1. Also, with your current indentation, you also increment i in the inner loop, so it'll reach d inside the inner loop causing the outer loop to only be run once too.

So, if d is 4, your values will go:

i = 0, s = 1
i = 1, s = 2
i = 2, s = 3
# exit inner loop
return True

You'd be much better off using for loops using range(), avoiding your mistakes altogether:

def check_sudoku(n):
    for i in range(len(n)):
        for s in range(1, len(n)):
            if n[0][i]==n[s][i]:
                return False
    return True

With range the inner loop will always start at 1, range through to len(n) every time without explicit resets needed.

If you use the any() function you can collapse the whole test into one line:

def check_sudoku(n):
    return not any(n[0][i] == n[s][i] for i in range(len(n)) for s in range(1, len(n)))

Upvotes: 2

Mike Vella
Mike Vella

Reputation: 10575

Further to unwind's answer, this is the modified code in a way that should work. I've changed the whitespace in the code and the operators in a way which most Python programmers will consider more conventional.

def check_sudoku(n):
    d = len(n)
    i = 0
    s = 1

    while i < d:
        print "i=",i
        while s < d:
            print "s=",s
            if n[0][i] == n[s][i]:
                return False
            s += 1
        i += 1
        s = 1
    return True 

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

check_sudoku(n)

Upvotes: 1

Thinh Phan
Thinh Phan

Reputation: 655

I think you should move the statement i=i+1 over the second loop follow as:

def check_sudoku(n):
d=len(n)
i=0
s=1
while i<d:
    print "i=",i
    while s<d:
        print "s=",s
        if n[0][i]==n[s][i]:
            return False
        s=s+1
    i=i+1
    return True

Upvotes: -1

unwind
unwind

Reputation: 399843

You don't reset s in the outer loop, so the inner loop will only execute once. After that, s < d will never be true again.

Upvotes: 3

Related Questions