[Tutor] Filling an array - with a twist

dn PyTutor at DancesWithMice.info
Wed Nov 11 14:06:40 EST 2020


Reading @wlfraed's response, I at-first wondered if it would lead to yet 
another approach to the problem of checking for limited-repetition:-


On 12/11/2020 07:06, Dennis Lee Bieber wrote:
> On Wed, 11 Nov 2020 16:30:54 +1000, Phil <phillor9 at gmail.com> declaimed the
> following:
...

> 	If any digit is only allowed twice (meaning 12344564 is rejected), then
> provide as input a sequence that gives each digit twice!
...

>>>> pool
> [0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6]


The point being that this is a "population" (an important definition in 
the study of Statistics), ie all of the values which may be chosen for a 
particular row/column - we can't add an 8, nor can we add another 0, 
because they are not within the specification.

Combine this with the set.remove(x) method which will "remove the first 
item from s where s[i] is equal to x".


Thus:
- generate a "pool" for every row and every column in the 8x8 'result'
- generate a new random() value
- remove it* from the applicable row and column pools
- add the value to the 8x8
* if either remove() fails (exception = ValueError), two examples of 
that number already exist in the applicable row/column
...


This is a "subtractive" approach to checking. Whereas previously we took 
an "additive" approach to deciding if the repetition rule(s) are about 
to be broken.
-- 
Regards =dn


More information about the Tutor mailing list