[Tutor] A dictionary question
Alan Gauld
alan.gauld at yahoo.co.uk
Fri Nov 19 07:14:46 EST 2021
On 19/11/2021 05:00, Phil wrote:
> The problem is, given a list of sets find a pair of numbers that occur
> in the same row and in no other set in that row.
>
> row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}]
>
> The pair in this set is {4, 6}. This is an easy one to solve because
> neither the 4 nor the 6 is in any other set.
Your explanation doesn't make sense to me.
row is a list of sets.
Your description says find a pair in the same row(ie same list of sets)
and in no other set in that row(list)
But 4,6 appears in 2 sets within the list?
> correct answer but it fails if the 4 or the 6 is repeated in another row
> set.
Again I'm not sure what that means. Does it mean repeated in a
different list of sets? or a different (third?) set within
the original row(list)?
> 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?
> I just had a bright idea while I'm typing this and the following code
> almost offer a complete solution:
>
> row = [{7,3},{5},{4,6,8},{7,8},{1},{9,3},{7,9},{4,6,3},{2}]
>
> number_list = []
> row_list = []
>
> for r in range(9):
> for i in range(1, 10):
> if i in row[r] and len(row[r]) > 2:
> number_list.append(i)
> row_list.append(r)
>
> for i in range(len(number_list)):
> print(number_list[i], end=' ')
>
> print()
>
> for i in range(len(row_list)):
> print(row_list[i], end=' ')
>
> At this point I have:
>
> 4 6 8 3 4 6
> 2 2 2 7 7 7
>
> What I need to do now is feed this result into a function that returns
> the tuple (4, 6). This is just for my own education and is not homework.
Again you've lost me. Why return 4,6 based on your data.
What makes that the right value?
And which 4,6?
There is a 4,6 pair at both ends of your first row?
Maybe if you can more precisely explain your algorithm and
rules we can provide more specific help.
I seem to recall you saying somewhere that it was a
sudoku puzzle? Is this still part of that?
If so it might help to describe your problem in
terms of the problem domain rather than your current
solution design? What do the sets represent? (cells,
and their guesses so far maybe?)
What are you trying to find within them?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list