Home > Software engineering >  How can I let my code return True or False based on the criteria set?
How can I let my code return True or False based on the criteria set?

Time:12-10

I asked this before but since I didn't get a very clear response as to where I should change my code, I'm gonna ask this here again :)

So I'm trying to analyze my list h and see whether for x in that list, are their two pairs of the first character in the elements or not

So for instance,

['AS', 'AD', 'CC', 'CH', 'CS'] returns False because there are 3 C and 2 , but there are supposed to be 2 of the same elements and one of a completely different element.

['AS', 'AD', 'SC', 'SH', 'CS'] returns True because there are 2 A, 2 S and one C.
['CS', 'CD', 'AC', 'AH', 'FS'] returns True because there are 2 C, 2 A and one F

['AS', 'CD', 'AC', 'CH', 'DS'] returns False because although there are two A, two C and one D, the A and C are not in order

Here is my code so far...

def two_pairs(h):
    for x in h:
        pairing = list[x[1]]
        if (pairing[1] == pairing[2]) and (pairing[3] == (pairing[4])):
            return True
        if (pairing[3] == pairing[4]) and (pairing[0] == (pairing[1])):
            return True
        if (pairing[0] == pairing[1]) and (pairing[3] == (pairing[4])):
            return True
        else:
            return False
print(two_pairs(['9r, 9♣, 4♠, 4♣, 10♣']))

This doesn't really work at all and I'm getting an error message that says

if (pairing[1] == pairing[2]) and (pairing[3] == (pairing[4])):
TypeError: There are no type variables left in list['r']

What should I change??

Note: the back characters can be anything from numbers to letters to special characters.

CodePudding user response:

h is a list of a string. This makes x a single string

Therefore x[1] == 'r' and list() is a built-in Python function that you are indexing with list['r'], which is not valid syntax for what you seem to want to do.

The answer you accepted in the previous post seemed to do what you want, and the input was a list of strings, not a list of one string.

e.g. Start with print(two_pairs(['9r', '9♣', '4♠', '4♣', 10♣'])), however none of these string have more than two characters, so it is going to still fail around pairing[2], for example

are their two pairs of the first character in the elements or not

Start with this

l = ['AS', 'AD', 'CC', 'CH', 'CS'] 
first = l[0][0]
print(2 == sum(x.count(first) for x in l))  # True

Or maybe

l = ['AS', 'AD', 'CC', 'CH', 'CS']
firsts = [x[0] for x in l]
print({f: sum(x.count(f) for x in l) for f in firsts})  # {'A': 2, 'C': 4}

CodePudding user response:

How about creating a dictionary where keys correspond to the first element of each string in the list and the values correspond to the indices.

Then when you iterate over the dictionary, you can return False if a value in a key appears more than 2 times or if they don't appear consecutively.

def check(l):
    d = {}
    for i, x in enumerate(l):
        d.setdefault(x[0],[]).append(i)

    for v in d.values():
        if len(v) > 2:
            return False
        elif len(v) == 2:
            if v[1] - v[0] == 1:
                continue
            else:
                return False
        else:
            continue
    return True

It returns the desired output at least for the examples you've provided.

  • Related