Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Making card configurations in Sagemath

I'm following SageMath's tutorial on Combinatorics (can't post link here cause I have low karma) and one of the exercizes is to calculate the set of Four of a Kind hands (in card games a hand containing four cards of the same value is called a four of a kind and a Hand is a subset of 5 cards). Each card has a Value and a Suit.

I want to generate the hand in a structured way like {(1,"Hearts"), (1,"Spades"), (1,"Clubs"), (1,"Diamonds"), (5,"Clubs") )}, so my idea was to create all the cards first and then join them in a set, like so:

Suits = Set(["Hearts", "Diamonds", "Spades", "Clubs"])
Values = Set([2, 3, 4, 5, 6, 7, 8, 9, 10,
              "Jack", "Queen", "King", "Ace"])
Cards = cartesian_product([Values, Suits])

pick = Values.random_element()
card1 = Subsets(cartesian_product([Set([pick]), Suits]),1)
card2 = Subsets(cartesian_product([Set([pick]), Suits]),1)
card3 = Subsets(cartesian_product([Set([pick]), Suits]),1)
card4 = Subsets(cartesian_product([Set([pick]), Suits]),1)
card5 = Subsets(Cards,1)

FourOfaKind = cartesian_product([card1,card2,card3,card4,card5])



However, I realised I'm picking Suits with reposition, which is not what I want!

How can i tell Sage Math that I don't want to pick the choice of the previous card? If i instantiate a Suit with Suit.random_element() each time I fix a card, then that won't be counted as part of the construction of the Suit/{whatever suit is instantiated} for the following cards, which puts me in quite a pickle! I'm sure there's some method to do this but I've been at hours for this and the Sage documentation is huge!

Any help would be really appreciated!