[Tutor] Filling an array - with a twist

Phil phillor9 at gmail.com
Wed Nov 11 20:22:29 EST 2020


Thank you everyone for your responses and there's a lot to digest.

Sets were mentioned and that's how I managed to write a Sudoku solver. 
However, in this case duplicates are allowed but not duplicates of more 
than 2, one after the other. I'd used the word sequence previously and 
it may have caused some confusion.

>> After thinking again, you should be able to do that with a simple-ish 
>> function that's passed the two relevant populations.  I'm not as fond 
>> of doing the removes as the check, because you have to fixitup - if 
>> it removed cleanly from row-population, and takes an exception from 
>> column-population, then you have to put it back into row-population 
>> and start the picking process again... I'd rather pick from one and 
>> check if the other population disqualifies that choice, something 
>> like this:
>>
>>
>> def select(row_pop, column_pop):
>>      """choose a value in both populations and remove it from both"""
>>      while True:
>>          x = random.choice(row_pop)
>>          if x not in column_pop:
>>              continue
>>          row_pop.remove(x)
>>          column_pop.remove(x)
>>          return x

This looks like a good place to start, I'll give it some thought and 
experiment further.

>
> Well done!
> PS "pop" is "population" not collection.pop()
>
> I didn't offer code, because I'm not sure if that would be doing the 
> OP's homework for him. Also, didn't worry about this (need for 
> "locking a transaction") because (a) it 'spoils' the smooth 
> description of the algorithm, and (b) leaving some 
> process-of-discovery for the coder.

My homework days are but a distant memory from the previous century. I'm 
grateful for all offerings.

-- 

Regards,
Phil



More information about the Tutor mailing list