Created
November 20, 2016 12:02
-
-
Save torbjo/29f6a0cdf4349c1a8bed52b4f6a89541 to your computer and use it in GitHub Desktop.
Cartesian product
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output