[Tutor] Creating a two dimension set

dn PyTutor at DancesWithMice.info
Mon Nov 1 18:23:43 EDT 2021



On 02/11/2021 10.28, Cameron Simpson wrote:
> On 01Nov2021 11:30, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>> On Mon, 1 Nov 2021 19:52:49 +1100, Cameron Simpson <cs at cskk.id.au>
>> declaimed the following:
>>
>>> I wouldn't use strings. If you're doing Sudoku don't you want to do
>>> arithmetic to check the sum? May as well make a set of ints:
>>>
>> 	No arithmetic done in Sudoku... You can replace the 1..9 with A..I, or
>> even emojis (if you can mentally keep track of such). The only thing one is
>> doing is eliminating candidates for a cell if that candidate was already
>> used on the row/column.
> 
> Ah, my Sudoku inexperience is revealed. Thanks for the correction!

May be, may be not!

Rule: each column and each row must include the digits one to nine. Each
digit appearing once and only once.

Thus, re-stating the above slightly, the easiest way to verify a
Sudoku's completeness (assuming the use of integers - and remembering
sum() ) is to add the contents of each column and each row. If such a
total is not 45, there's something wrong!

NB I saw a code-competition where one ever-so keen fellow took this idea
and figured that the 9x9 tableau should also sum() to a unique number.
Was very proud of his 'improvement' until it was pointed-out that (if
that was the only check) complementary-errors would be 'missed'...


Sudoku, even the tic-tac-toe/noughts-and-crosses of my youth, is a very
good problem to stretch peoples' minds regarding concepts of
data-visualisation and thus data-structures. The most obvious approach
is probably to figure-out how to represent the tableau with a bunch of
digits arranged in rows and columns (just as on-paper).

However, some solutions make very clever use of indices/indexing. In
which case the 'actual data', can be a simple list() or dict().


That said, the OP's idea of comparing a column (say) as a list, with the
same list coerced to a set and then len()-s compared, is a good way to
detect duplicates!
-- 
Regards,
=dn


More information about the Tutor mailing list