Skip to content

Instantly share code, notes, and snippets.

@martin-martin
Created May 6, 2023 00:47
Show Gist options
  • Save martin-martin/1008796754d42ec2487874529304803f to your computer and use it in GitHub Desktop.
Save martin-martin/1008796754d42ec2487874529304803f to your computer and use it in GitHub Desktop.
import numpy as np
def create_deck():
RANKS = '2 3 4 5 6 7 8 9 10 J Q K A'.split()
SUITS = '♣ ♢ ♡ ♠'.split()
return np.array([r + s for s in SUITS for r in RANKS])
print(create_deck())
# ['2♣' '3♣' '4♣' '5♣' '6♣' '7♣' '8♣' '9♣' '10♣' 'J♣' 'Q♣' 'K♣' 'A♣' '2♢'
# '3♢' '4♢' '5♢' '6♢' '7♢' '8♢' '9♢' '10♢' 'J♢' 'Q♢' 'K♢' 'A♢' '2♡' '3♡'
# '4♡' '5♡' '6♡' '7♡' '8♡' '9♡' '10♡' 'J♡' 'Q♡' 'K♡' 'A♡' '2♠' '3♠' '4♠'
# '5♠' '6♠' '7♠' '8♠' '9♠' '10♠' 'J♠' 'Q♠' 'K♠' 'A♠']
@martin-martin
Copy link
Author

Also gives you a nice opportunity to show what .permutation(), .shuffle(), and .permuted() do:

>>> stacked_deck = create_deck().reshape(4, 13)
>>> stacked_deck
array([['2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣', 'A♣'],
       ['2♢', '3♢', '4♢', '5♢', '6♢', '7♢', '8♢', '9♢', '10♢', 'J♢', 'Q♢', 'K♢', 'A♢'],
       ['2♡', '3♡', '4♡', '5♡', '6♡', '7♡', '8♡', '9♡', '10♡', 'J♡', 'Q♡', 'K♡', 'A♡'],
       ['2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♠']], dtype='<U3')
>>> rng.permutation(stacked_deck)
array([['2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♠'],
       ['2♢', '3♢', '4♢', '5♢', '6♢', '7♢', '8♢', '9♢', '10♢', 'J♢', 'Q♢', 'K♢', 'A♢'],
       ['2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣', 'A♣'],
       ['2♡', '3♡', '4♡', '5♡', '6♡', '7♡', '8♡', '9♡', '10♡', 'J♡', 'Q♡', 'K♡', 'A♡']], dtype='<U3')
>>> stacked_deck
array([['2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣', 'A♣'],
       ['2♢', '3♢', '4♢', '5♢', '6♢', '7♢', '8♢', '9♢', '10♢', 'J♢', 'Q♢', 'K♢', 'A♢'],
       ['2♡', '3♡', '4♡', '5♡', '6♡', '7♡', '8♡', '9♡', '10♡', 'J♡', 'Q♡', 'K♡', 'A♡'],
       ['2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♠']], dtype='<U3')
>>> rng.permutation(stacked_deck, axis=1)
array([['2♣', '6♣', 'K♣', '9♣', 'J♣', '3♣', '8♣', 'Q♣', '5♣', '10♣', '7♣', '4♣', 'A♣'],
       ['2♢', '6♢', 'K♢', '9♢', 'J♢', '3♢', '8♢', 'Q♢', '5♢', '10♢', '7♢', '4♢', 'A♢'],
       ['2♡', '6♡', 'K♡', '9♡', 'J♡', '3♡', '8♡', 'Q♡', '5♡', '10♡', '7♡', '4♡', 'A♡'],
       ['2♠', '6♠', 'K♠', '9♠', 'J♠', '3♠', '8♠', 'Q♠', '5♠', '10♠', '7♠', '4♠', 'A♠']], dtype='<U3')
>>> stacked_deck
array([['2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣', 'A♣'],
       ['2♢', '3♢', '4♢', '5♢', '6♢', '7♢', '8♢', '9♢', '10♢', 'J♢', 'Q♢', 'K♢', 'A♢'],
       ['2♡', '3♡', '4♡', '5♡', '6♡', '7♡', '8♡', '9♡', '10♡', 'J♡', 'Q♡', 'K♡', 'A♡'],
       ['2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♠']], dtype='<U3')

For example. Then with .shuffle() you can show that it works in-place. Also makes sense to discuss the axis parameter on it, if you want to highlight the differences and similarities.

@martin-martin
Copy link
Author

Or, if this is too overwhelming, you could make a selection of cards first. For example, the Austrian card game called Schnapsen is played with only 10 J Q K A:

>>> schnapsen_deck = stacked_deck[:,-5:]
>>> schnapsen_deck
array([['10♣', 'J♣', 'Q♣', 'K♣', 'A♣'],
       ['10♢', 'J♢', 'Q♢', 'K♢', 'A♢'],
       ['10♡', 'J♡', 'Q♡', 'K♡', 'A♡'],
       ['10♠', 'J♠', 'Q♠', 'K♠', 'A♠']], dtype='<U3')

With this deck, you'd (nearly) be back to the amount of elements that you're using in your examples, but it adds a bit of real-world meaning to it.

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