# function to find largest 2 n_tuples and output them in a set (e.g. full house outputs [3,2]) def n_tuple(hand): count1 = 1 for j in range(1,5): if hand[0][0] == hand[j][0]: count1 = count1 + 1 count2 = 1 for j in range(2,5): if hand[1][0] == hand[j][0]: count2 = count2 + 1 count3 = 1 for j in range(3,5): if hand[2][0] == hand[j][0]: count3 = count3 + 1 count4 = 1 for j in range(4,5): if hand[3][0] == hand[j][0]: count4 = count4 + 1 import heapq return heapq.nlargest(2, (count1, count2, count3, count4)) # Function returns True is hand is a flush def is_flush(hand): result = 0 for i in range(1,5): if hand[0][1] == hand[i][1]: result = result +1 if result == 4: return True else: return False # Function returns True is hand is a straight def is_straight(hand): if n_tuple(hand)[1]!=1: return False else: hand = sorted(hand) count = 1 for i in range(1,5): if hand[i-1][0] == hand[i][0]-1: count = count + 1 if count == 5: return True else: return False # Function returns high card def high_card(hand): return 1 # manually set poker hand poker_hand = [[1,"A"],[3,"A"],[2,"A"],[4,"A"],[5,"A"]] print poker_hand print n_tuple(poker_hand) print is_flush(poker_hand) print is_straight(poker_hand) print high_card(poker_hand)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question