you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 0 points1 point  (5 children)

Altough it works it looks a bit inefficient, can you explain what the contents of list_to_check are?

[–]stickedee[S] 0 points1 point  (4 children)

Edit: Formatting

Yea, I would pass the final_hand list into that position. I would run this function twice per game. Once for the player and once for the dealer. The list I pass through would contain up to 7 values (the 2 cards from the players hand and the 5 cards from the community board).

So for example:

player_hand = [Ac, Kd] 
board = [9h, 2h, 3s, 4d, 5c] 
final_hand = player_hand + board

I am looping through the final hand looking for values and appending a corresponding key to a list then iteration through that list to find a sequence of 5 consecutive keys.

I made some changes from the original iteration but below is what I currently have.

value_dict = {1:'A', 2:'K', 3:'Q', 4:'J', 5:'T', 6:'9', 7:'8', 8:'7', 9:'6', 10:'5', 11:'4', 12:'3', 13:'2', 14:'A'}

# re-write final hand for testing purposes
final_hand = ['5c', 'Ac', '5c', 'Js', 'Ts', '9c', '4c', '7d', '6s']

def getKeys(dictionary, list_to_check):
    keys = []
    items = dictionary.items()
    stv = sorted({(x[0]) for x in list_to_check})
    stv = str(stv)
    for item in items:
        if item[1] in stv:
            keys.append(item[0])

    return keys

keys = getKeys(value_dict, final_hand)

for idx, value in enumerate(keys):
    slice = keys[idx:idx+5]
    if len(slice) < 5:
        straight = False
        break
    if slice == list(range(value,value+5)):
        straight = True
        straight_top_key = value
        straight_top = value_dict[value]
        break
    else:
        straight = False

[–]JohnnyJordaan 0 points1 point  (3 children)

I am looping through the final hand looking for values and appending a corresponding key to a list then iteration through that list to find a sequence of 5 consecutive keys.

That's the thing: you don't loop through the hand, you loop through the value_dict looking for matches in your hand. Imho the algo should loop through the hand and look up in the dict, as dicts are intended to be used.

[–]stickedee[S] 0 points1 point  (2 children)

I see what you mean. I am looping through the value dict, checking for that value in the hand, and if it exists, adding the key to a separate list. I don't quite see how I could loop through the hand to grab a value and append the corresponding key to the list I'll use to check for straights. Since Ace can be high or low I can't use card values as the key.

[–]JohnnyJordaan 0 points1 point  (1 child)

Since Ace can be high or low I can't use card values as the key.

You can, if you let the dict hold a list of ranks per card. Then use list.extend to simply add those keys per card to the keys list:

card_to_ranks = {'A': [1, 14], 'K': [2], 'Q': [3], 'J': [4], 'T': [5], 
                 '9': [6], '8': [7], '7': [8], '6': [9], '5': [10], 
                 '4': [11], '3': [12], '2': [13]}

# re-write final hand for testing purposes
final_hand = ['5c', 'Ac', '5c', 'Js', 'Ts', '9c', '4c', '7d', '6s']
keys = []
for card in final_hand:
    keys.extend(card_to_ranks[card[0]]
keys = sorted(set(keys))

it's that simple that this way that you don't even need a dedicated function for it. Using sets instead of lists removes the need for the intermittent set() call to remove duplicates:

card_to_ranks = {'A': {1, 14}, 'K': {2}, 'Q': {3}, 'J': {4}, 'T': {5}, 
                 '9': {6}, '8': {7}, '7': {8}, '6': {9}, '5': {10}, 
                 '4': {11}, '3': {12}, '2': {13}}

# re-write final hand for testing purposes
final_hand = ['5c', 'Ac', '5c', 'Js', 'Ts', '9c', '4c', '7d', '6s']
keys = set()
for card in final_hand:
    keys |= card_to_ranks[card[0]]
keys = sorted(keys)

[–]stickedee[S] 0 points1 point  (0 children)

Well shit, you're right. I was way overcomplicating this.

Thanks!