Ask Your Question
1

Making card configurations in Sagemath

asked 2023-06-21 02:13:05 +0100

Luisz gravatar image

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])

FourOfaKind.cardinality()

FourOfaKind.random_element()

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!

edit retag flag offensive close merge delete

Comments

John Palmieri gravatar imageJohn Palmieri ( 2023-06-21 22:30:13 +0100 )edit

The exercise gives a hint: use Arrangements(Values, 2) to get two values, and then choose a suit for the first value. Then the hand will be (1st value + suit, 4 of a kind of 2nd value).

John Palmieri gravatar imageJohn Palmieri ( 2023-06-21 22:35:10 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2023-06-21 22:43:27 +0100

Using what you have so far:

Suits = Set(["Hearts", "Diamonds", "Spades", "Clubs"])
Values = Set([2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King", "Ace"])
X = Arrangements(Values, 2)  # two values: first will be the singleton, second will be 4 of a kind

Now we can construct the hands in several ways. Using a loop:

Hands = []
for a in X:
    for s in Suits:
        first_card = (a[0], s)
        rest = [(a[1], Suits[i]) for i in range(4)]
        hand = [first_card] + rest
        Hands.append(hand)

Or more briefly using list comprehension:

Hands = [[(a[0], s)] + [(a[1], Suits[i]) for i in range(4)] for a in X for s in Suits]
edit flag offensive delete link more

Comments

So, essentialy, we build the entire set of Hands from scratch! (computational bruteforce) Brilliant! Thank you very much for the answers, I found it very enlightening indeed!

Luisz gravatar imageLuisz ( 2023-06-24 11:00:39 +0100 )edit
1

answered 2023-06-21 14:39:21 +0100

Max Alekseyev gravatar image

Here is a way:

 FourOfaKind = [ {(v,s) for s in Suits} | {(v2,s2)} for v,v2 in Permutations(Values,2) for s2 in Suits ]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-06-21 02:13:05 +0100

Seen: 175 times

Last updated: Jun 21 '23