Not Equal to Each Other?

Bengt Richter bokr at oz.net
Thu Nov 3 22:08:05 EST 2005


On 3 Nov 2005 17:01:08 -0800, ale.of.ginger at gmail.com wrote:

>Another question:  I am writing a sudoku solving program.  The
>'solving' part of is just multiple iterations.  It will take random
>numbers and keep switching it all around until a set of logic
>statements has been met (ie; all numbers in a row are not equal to each
>other) ... that's where my question comes in.
>
>Cellboard = my list for storing each row/column's data.
>
>Rather than writing
>
>cellboard[0] is not* (cellboard[1] and cellboard[2] and cellboard[3]
>and cellboard[4] ... cellboard[8])
>cellboard[1] is not (cellboard[0] and cellboard[2] and cellboard[3] and
>cellboard[4] ... cellboard[8])
>etc...
>
>* should this be != ?
>
>the above so that all the data in one row is not equal to each other,
>is there something I can write to make it simpler?  For example,
>(cellboard[0] is not cellboard[1] is not ... cellboard[8]) only worked
>for the numbers to the left and right of the cell - is there anyway I
>can expand this to cover all numbers in a set range?
>

UIAM if you have a list of items that are comparable and hashable, like integers,
you can make a set of the list, and duplicates will be eliminated in the set.
Therefore if the resulting set has the same number of members as the list it
was made from, you can conclude that the list contains no duplicates. E.g.,

 >>> cellboard = range(8)
 >>> cellboard
 [0, 1, 2, 3, 4, 5, 6, 7]
 >>> set(cellboard)
 set([0, 1, 2, 3, 4, 5, 6, 7])
 >>> len(set(cellboard))
 8
 >>> cellboard[2] = 7
 >>> cellboard
 [0, 1, 7, 3, 4, 5, 6, 7]
 >>> set(cellboard)
 set([0, 1, 3, 4, 5, 6, 7])
 >>> len(set(cellboard))
 7

So the test would be
 >>> len(set(cellboard))==len(cellboard)
 False
And after repairing the list to uniqueness of elements:
 >>> cellboard[2] = 2
 >>> len(set(cellboard))==len(cellboard)
 True

HTH

Regards,
Bengt Richter



More information about the Python-list mailing list