I'm writing a passion program that will determine the best poker hand given hole cards and community cards. As an ace can go both ways in a straight, I've coded this as [1, 14] for a given 5 card combination.
I understand recursion but implementing it is a different story for me. I'm looking for a function that will split all aces recursively, into all possible hand combinations until all nested lists are exhausted. This should obviously work with up to 4 aces, overlooking the fact that you wouldn't care about a straight at that point in all likelihood.
hand = [[1, 14], 2, 3, [1, 14], 7]
desired_output = [
[1, 2, 3, 1, 7],
[1, 2, 3, 14, 7],
[14, 2, 3, 1, 7],
[14, 2, 3, 14, 7]
]
I'm not proud of what I have so far, especially because it returns a list instead of something like a yield
which would build the list I'm looking for:
def split_first_ace(hand):
aces = [True if isinstance(x, list) else False for x in hand]
for i, x in enumerate(aces):
if x:
ranks_temp = hand.copy()
ace = ranks_temp.pop(i)
return [[ace[0]] + ranks_temp, [ace[1]] + ranks_temp]
Any help on a solution would be appreciated, mostly because it'll help me understand how to implement recursion. But I'm open to other solutions as well.