Skip to content

Instantly share code, notes, and snippets.

@torbjo
Created November 20, 2016 12:02
Show Gist options
  • Save torbjo/29f6a0cdf4349c1a8bed52b4f6a89541 to your computer and use it in GitHub Desktop.
Save torbjo/29f6a0cdf4349c1a8bed52b4f6a89541 to your computer and use it in GitHub Desktop.
Cartesian product
import random
def cartesian_product (seq1, seq2):
return [a+b for a in seq1 for b in seq2]
# A deck of cards
deck = cartesian_product ('♠♥♦♣', list('123456789') + ['10'] + list('JQK'))
def shuffle_cards ():
random.shuffle (deck)
def draw_hand (ncards=5):
hand = random.sample (deck, ncards)
for card in hand: deck.remove (card)
return hand
def draw_card ():
card = random.choice (deck)
deck.remove (card)
return card
shuffle_cards()
player1 = draw_hand (5)
player2 = draw_hand (5)
print ('Your hand', ' '.join (player1))
answer = input ('Throw which cards? ')
for i in [int(s) for s in answer.split()]:
player1[i-1] = draw_card()
print ('Your hand', ' '.join (player1))
@torbjo
Copy link
Author

torbjo commented Nov 20, 2016

Output

Your hand ♣J ♥J ♥Q ♠4 ♥6
Throw which cards? 4 5
Your hand ♣J ♥J ♥Q ♣Q ♣K

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment