[Tutor] A dictionary question

Mats Wichmann mats at wichmann.us
Fri Nov 19 10:09:52 EST 2021


On 11/19/21 05:14, Alan Gauld via Tutor wrote:

>> There is no solution for the follow row because there is no unique pair:
>>
>> row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}]
> 
> Why not 4,5? it appears in 2 sets, which seems to be the criteria?

because 5 appears by itself in several other sets, as does 4 (that 
second qualifier was not, if I recall, in the original description)

===

and since my partial bit the other day didn't lead to a finished 
solution, I'll repaste it with the second half also included (not 
prettied up):

from collections import Counter

row = [{7,3},{5},{4,8,6},{7,8},{1},{9,3},{7,9},{4,6,3},{2}]
#row = [{5},{6},{3},{4,7,5},{1,2,4,5},{1,7,5},{1,2,4,7,9},{8},{1,2,4,7,9}]

set_list = [{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9},
             {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9},
             {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9},
             {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4,9},
             {5, 6}, {5, 7}, {5, 8}, {5, 9},
             {6, 7}, {6, 8}, {6, 9},
             {7, 8}, {7, 9},
             {8, 9}
             ]

pairs = Counter()

for s in set_list:
     for r in row:
         if s.issubset(r):
             pairs[tuple(sorted(s))] += 1

for item, count in pairs.items():
     if count == 2:  # candidate
         i, j = item
         # since we know we're only counting two things,
         # no need to use a Counter or dict here
         icount, jcount = 0, 0
         for r in row:
             if i in r:
                 icount += 1
             if j in r:
                 jcount += 1
         if icount == 2 and jcount == 2:
             print(f"{set(item)} found")


More information about the Tutor mailing list