Python Forum
trying to put a a filter on identifying a straight - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: trying to put a a filter on identifying a straight (/thread-35655.html)



trying to put a a filter on identifying a straight - CompleteNewb - Nov-27-2021

I have written this code to identify possible straights in a serie of numbers. It works find thanks to the help of the people in this forum, but now I want this function to only identify possible straights with 3 items in the set.

flop = [7, 8, 9]
possiblestraight = []

def check3outof3straightinflop(flop, possiblestraightcheck, possiblenumberofstraight, possiblestraight, possiblestraightgradelist):
    possiblestraightgrade = []
    possibleplayershand = []

    for n in range(len(flop)):
        possibleplayershand.append(int(flop[n][0]))

    possibleplayershand.sort()
    
    if possibleplayershand[0] == 1:
        possibleplayershand.append(14)
   
    cardsset = set(possibleplayershand)

    for f in cardsset:
        possiblestraightset = cardsset.intersection(range(f,f+5))
        possiblestraightlist = list(possiblestraightset)
        possiblestraightlist.sort()
        if len(possiblestraightlist) > 2:
            possiblestraight.append(possiblestraightlist)
            
    print(possiblestraight)

#The if statement above here is where the [8,9] is supposed to be removed 

    possiblestraightset =  []
    for s in possiblestraight:
        possiblestraightset.append(set(s))

    possiblestraight = []
    for s in possiblestraightset:
        for r in possiblestraight:
            if s.issubset(r):
                break
        else:
            possiblestraight.append(s)
        
    for n in possiblestraight:
        if n != 0:
            possiblestraightgrade = max(n)
            possiblestraightgradelist.append(possiblestraightgrade)
    possiblestraightgradeset = set(possiblestraightgradelist)

    if possiblestraight != []:
        possiblestraightcheck = True

    possiblenumberofstraight = len(possiblestraight)
    
    return(possiblestraightcheck, possiblestraight, possiblestraightgradeset, possiblenumberofstraight)
Output:
(True, [{8, 9}, {8, 9, 7}], {9}, 2)
So if the flop = [7, 8, 9], i want my funtion to return the list "possiblestraight" as [{8,9,7}] because the set [8,9] is not 3 items long so it should be removed, but the problem is that it's not "possiblestraight = [{8, 9}, {8, 9, 7}]"

Now I 've written this to check if my logic was okay

possiblestraight = []
possiblestraightset = []
possiblestraightlist = []
possibleplayershand = [7, 8, 9]


cardsset = set(possibleplayershand)
for f in cardsset:
    possiblestraightset = cardsset.intersection(range(f,f+5))
    possiblestraightlist = list(possiblestraightset)
    possiblestraightlist.sort()
    if len(possiblestraightlist) > 2:
        possiblestraight.append(possiblestraightlist)
            
print(possiblestraight)
Output:
[[7, 8, 9]]
and it works, my result is [[7, 8, 9]]
So what's the problem?
I have been trying to find the solution for a week now and i can't find it, please help me.


RE: trying to put a a filter on identifying a straight - CompleteNewb - Dec-01-2021

Found the problem, the element with less than 3 items came from a previous function call, so the list wasn't empty when it was call in this function... sorry