Home > other >  How can I make my output return True or False when the input is a set of tuples?
How can I make my output return True or False when the input is a set of tuples?

Time:12-11

So I'm trying to analyze my set of tuples h and see whether for x in that set, are there two pairs of 2 elements and 3 elements each respectively.

So like for every first character there must be 3 identical characters and 2 identical characters...

They must be connected though, like if there are 3 A and 2 C, the order can only be AAACC or CCAAA, not CACAA or ACCAA etc

So for instance,

{('AS', 'AD', 'CC', 'CH', 'CS')} returns True because there are 3 C and 2 A
{('AS', 'AD', 'SC', 'SH', 'CS')} returns False because there are 2 A, 2 S and one C.
{('CS', 'CD', 'CC', 'AH', 'AS')} returns True because there are 3 C and 2 A 

{('AS', 'DD', 'AC', 'AH', 'DS')} returns False because even though there are 3 A and 2 D, they are not following each other as indicated by the bold text above

Here is my attempt at the code

def is_full_house(h):
    h = list(h)
    for hands in h:
        bools = True
        last_count = 1
        last_item = hands.pop(0)[0]
        while hands:
            item = hands.pop(0)
            if (item[0] == last_item):
                last_count  = 1
            if (last_item != item[0]):
                if (last_count == 2) or (last_count == 3):
                    bools = True
                else:
                    bools = False
                    break
                last_item = item[0]

        return (bools)

However, my code only works when it's a list... when I put in a set of tuples, it doesn't work...

What changes should I make for it to work?

Edit: My output says error, tuple has no attribute pop. However, I've tried to convert it into a list already as shown by h = list(h), but still it doesn't work

CodePudding user response:

The structures that you are passing in to the function, like {('AS', 'AD', 'CC', 'CH', 'CS')}, are sets of tuples, so you will need to have a line to convert to a list inside the for loop as well:

for hands in h:
    hands = list(hands)

CodePudding user response:

h = list(h)

Is only changing the set to a list. If you want to change the individual tuples within your set you would need to do

h = [list(hands) for hands in h]

The reason why it doesn't work with tuples is because tuples cannot be modified. This is done to make tuples more performant, and it is for this reason that tuples do not have a pop function.

  • Related