PYTHON A card is a tuple containing a number between 1 and 13 representing the c
ID: 3819378 • Letter: P
Question
PYTHON
A card is a tuple containing a number between 1 and 13 representing the cards value and a string containing "clubs", "diamonds", "hearts", or "spades". Thus the ace of spaces would look like (1, "spades") and the king of diamonds (13, "diamonds") A deck is a list of 13 cards.
Your program should create a deck of cards, shuffle it, and then put the first five cards in the deck into a 5 element list which represents a hand.
You should write a series of functions which return boolean values that can be used to analyze this hand.
def is_pair(hand): should return true if there are exactly 2 cards of the same value
def is_2_pair(hand): should return true if there is one set of 2 cards with a common value and a second set of 2 cards with a different common value
def is_3_ofakind(hand): returns true if there are exactly 3 cards with a comrmon value
def is_full_house(hand): returns true if there are 3 cards with a common value and the other cards share a different common value
def is_flush(hand): if the five cards all have the same suit
def is_straight(hand): returns true if the value of the five cards form a sequence which increases by 1 in each case. For instance (2,"hearts"). (3,"spades"), (4, "diamonds"), (5,"hearts"), (6,"clubs")
Your program should create a deck, shuffle it, create a hand from the first five cards, then call each of the above functions and print out which of them return True.
Explanation / Answer
import random
def gendeck():
decklist = []
for i in range(1, 14):#deck of spade
decklist.append((i, 'spade'))
for i in range(1, 14):#deck of diamond
decklist.append((i, 'diamond'))
for i in range(1, 14):#deck of club
decklist.append((i, 'club'))
for i in range(1, 14):#deck of heart
decklist.append((i, 'heart'))
return decklist#return deck of cards
def shuffle(decklist):
size = len(decklist)
for i in range(size - 1, 0, -1):
pos = random.randint(0, i)#get a random position
decklist[i], decklist[pos] = decklist[pos], decklist[i]#swap i with random pos
def is_straight(hand):
marker = [False]
marker = marker * 14
for tpl in range(len(hand)):
marker[hand[tpl][0]] = True# maek card value true
#check if marker contains 5 consecutive true value
for i in range(4, len(marker)):
if marker[i] == True:
count = 0
for j in range(i, i - 5, -1):
if marker[j] == False:
break
count += 1
if count == 5:#if count = 5 means 5 consecutive True found return true
return True
return False
def is_flush(hand):
for tpl in range(1, len(hand)):
if hand[tpl][1] != hand[tpl - 1][1]:#check if all card belong to smae suite
return False
return True
def is_3_ofakind(hand):
marker = [0]
marker = marker * 14
for tpl in range(len(hand)):
marker[hand[tpl][0]] += 1
if 3 in marker:#check if 3 common element are found
return true
return False
def is_full_house(hand):
marker = [0]
marker = marker * 14
for tpl in range(len(hand)):
marker[hand[tpl][0]] += 1
if 3 in marker and 2 in marker:#check if 3 and 2 common element are found
return true
return False
def is_pair(hand):
marker = [0]
marker = marker * 14
for tpl in range(len(hand)):
marker[hand[tpl][0]] += 1
if marker.count(2) == 1:#check if only one pair is there
return True
return False
def is_2_pair(hand):
marker = [0]
marker = marker * 14
for tpl in range(len(hand)):
marker[hand[tpl][0]] += 1
if marker.count(2) == 2:#check if 2 pair is there
return True
return False
def main():
decklist = gendeck()
shuffle(decklist)
hand = decklist[:5]
print (hand)
print ("is_2_pair", is_2_pair(hand))
print ("is_pair", is_pair(hand))
print ("is_full_house", is_full_house(hand))
print ("is_3_ofakind", is_3_ofakind(hand))
print ("is_flush", is_flush(hand))
print ("is_straight", is_straight(hand))
main()
Output:
>>>
RESTART: C:/Users/IBM_ADMIN/AppData/Local/Programs/Python/Python36/cheeg.py
[(5, 'spade'), (6, 'club'), (11, 'heart'), (2, 'diamond'), (4, 'heart')]
is_2_pair False
is_pair False
is_full_house False
is_3_ofakind False
is_flush False
is_straight False
>>>
>>>
RESTART: C:/Users/IBM_ADMIN/AppData/Local/Programs/Python/Python36/cheeg.py
[(6, 'spade'), (1, 'diamond'), (3, 'spade'), (9, 'spade'), (10, 'diamond')]
is_2_pair False
is_pair False
is_full_house False
is_3_ofakind False
is_flush False
is_straight False
>>>
RESTART: C:/Users/IBM_ADMIN/AppData/Local/Programs/Python/Python36/cheeg.py
[(3, 'spade'), (13, 'club'), (10, 'spade'), (1, 'spade'), (6, 'diamond')]
is_2_pair False
is_pair False
is_full_house False
is_3_ofakind False
is_flush False
is_straight False
>>>
RESTART: C:/Users/IBM_ADMIN/AppData/Local/Programs/Python/Python36/cheeg.py
[(8, 'spade'), (9, 'heart'), (9, 'club'), (12, 'spade'), (1, 'club')]
is_2_pair False
is_pair True
is_full_house False
is_3_ofakind False
is_flush False
is_straight False
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.