[Tutor] The Python way and two dimensional lists

Alan Gauld alan.gauld at yahoo.co.uk
Tue Dec 7 20:10:56 EST 2021


On 08/12/2021 00:31, Phil wrote:

> The second list of lists represents the Tkinter GUI where the user can 
> enter a puzzle to be solved. The programme solves the puzzle, not the user.

I try to keep the presentation logic as far away from
the game logic as possible. Usually drawing a user friendly
picture of algorithmic data is easier than forming algorithms
around user friendly layouts!

>> I'd just use a single list of 81 cells.

> I'll think about this. As dn pointed out, knowing where the line endings 
> are could be a complication.

That's why you need the helper function. It abstracts the
underlying data structure from the higher level concept
of solving the problem per row, column and box. This keeps
the game rules separate from the slicing and dicing of
data collection.

Each function (row/column/box returns the appropriate list
of cells. The validation is the exact same for each, you
just pass the list of 9 cells to the validator function

> I'm a little reluctant to move from the model that I have because I 
> understand how it represents the board, I'm just having a problem with 
> manipulating the board grid to extract the columns. 

Exactly, you are trying to separate rows/columns and boxes
all in one place while simultaneously trying to apply the
rules. That's a lot of mental concepts to juggle in one
piece of code.

> I'd then write helper functions to extract the sublists I needed:
>>
>> def get_cell(table, row, col)
>>
>> def get_row(table, row_num)
>>
>> def get_col(table, col_num)
>>
>> def get_box(table, row_num, col_num)  # or just box_num?
> 
> I don't have a function that just gets a column. Column extraction is 
> performed as part of other functions. I know that a function should do 
> just one job, so I'll work on that. I have started a function that gets 
> the box number but I cannot see, at the moment, how to integrate it with 
> the rest of my programme.

As I understand it you need to check rows columns and boxes.
If its an interactive game you might just need to check the
move for validity and completion of the puzzle - thats much
easier than actually solving the puzzle.

But if you are catering for interactive play rather than
purely solving the puzzle then you probably need helper
functions that can return the required items for any given
cell anyway, so those helpers get used both in checking
and in solving.

>> The next problem (based on previous posts) is that your
>> cells are sets which are immutable, so you need to:
>> 1) create another set of functions for writing back changes
>> to a cell - tricky...
> 
> I'm not sure that I understand why this is difficult. This is how I 
> update a cell:
> 
> self.solution[row][col] = set(num)

I'm not entirely sure what the set holds, I've assuming the set of
possible values, which reduces in size as other cells get completed?
The problem is you cannot easily edit a cell directly, you need to
extract the set, create a new set and then assign the new set back to
the cell. If you use a list instead you can address the cell list
directly and edit in place.

For example if you just use bare sets in a list:

sl = [{1,2},{3,4}]
for st in sl:
    sl = sl + 9  # uh uh, immutable set

Does not modify the original values in sl.
You need to use the index and replace the original list
with a new one.

But using a list:

ll = [[1,2],[3,4]]
for lst in ll:
    lst.append(9)   ## ok, mutable list

Does modify the original list. Much simpler code.

>> 3) Use OOP and make your cells objects with methods
>> Personally, I'd use OOP for the table too, and make the
>... 
> A grid class would probably be a good idea, more to think about.

I notice you said to dn that this originally was C++ so I'll
assume classes and objects are familiar even though you haven't
used them thus far. Classes could simplify the top level code
but will not make the overall code any shorter. But they
should hide the more murky details.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list