Why isn't this loop working?

I'm trying to solve the Suduko problem by using a nested loop, and the zip function. When I run the initial list/matrix through my loop, it returns the correct values. However, when I run the zipped version of the list through the exact same loop (or a copy redefined as a separate function), it tells me it can't pop because there aren't any values in the list. I really don't understand why that is. By creating a new list with the the zip function, shouldn't it have values in it? And, when I run my code through the visual interpreter at: http://people.csail.mit.edu/pgbovine/python/tutor.html#mode=edit, it even displays the zipped matrix as a list.

I'd post the code here, but I know we're not supposed to do that.

asked 12 Mar '12, 10:38

marimbadaddy's gravatar image

marimbadaddy
7718
accept rate: 20%

1

With all due respect, why are you trying to incorporate functions not yet presented in order to solve the homework? Especially if they are increasing the difficulty of assignment?

(12 Mar '12, 12:56) pmoriarty pmoriarty's gravatar image

It seemed that the zip function was a quick and easy way to get the data represented by the columns, as we had the data listed in rows. Also, it seems logical that if one sums both the columns and the rows, then compares these sums, it is the easiest way to test whatever size Suduko square. Just testing the rows wouldn't work because you can have all of correct numbers in the puzzle, but not in the correct order.

(12 Mar '12, 13:17) marimbadaddy marimbadaddy's gravatar image
1

Have you tried printing the zipped list to verify that it does have values (in the interpreter where you are having problems)?

(12 Mar '12, 13:36) Kenneth I. L... Kenneth%20I.%20Laws-1's gravatar image

I agree with others here. You should try to stick with the functions we know as of now. Even though some other functions could make the problem a lot easier, we are not trying to make things easy, we are trying to learn. Making a somewhat complicated but interesting and nuanced solution is what we are aiming for. We aren't looking for the best possible way to check if something is a sudoku, we are leaning how to execute various random tasks for the sake of learning.

(12 Mar '12, 13:45) Josh Melnick Josh%20Melnick's gravatar image

When dealing with sums of columns and rows, make sure your code will handle something like [[1,4,4,1],[4,1,1,4],[4,1,1,4],[1,4,4,1]]

(12 Mar '12, 14:02) jane jane's gravatar image

One Answer:

I think i found your problem, I used the Python interpreter and it seems the ZIP function changes your (list of lists) matrix into a list of tuples.

Tuples are a different data structure in Python, represented by enclosing the element(s) in parenthesis. You might need to change your code to adapt to this.

Or you could use nested cycles (for) to transpose the matrix (I would assign this to a new variable) manually and run your code normally.

NOTE: Also you must take into accound that a 4x4 sudoku might have a number 5 or 0 in it, making it an incorrect sudoku square =)

link

answered 12 Mar '12, 13:49

SaidMC's gravatar image

SaidMC
514

Thanks! I believe your are 100% correct. I was banging my head not understanding why the data looked correct, yet wouldn't pass through.

(12 Mar '12, 18:31) marimbadaddy marimbadaddy's gravatar image

tuples are simply immutable lists so if marimbadaddy isn't attempting to modify the tuples there shouldn't be an issue. In the grander sense tuples are more than immutable lists but that will probably be covered if/when the course covers the structure.

(12 Mar '12, 18:46) David Smith David%20Smith's gravatar image
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,276
×195
×46
×13

Asked: 12 Mar '12, 10:38

Seen: 206 times

Last updated: 12 Mar '12, 18:46