ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 24 Jun 2023 11:00:39 +0200Making card configurations in Sagemathhttps://ask.sagemath.org/question/69313/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])
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!Wed, 21 Jun 2023 02:13:05 +0200https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/Comment by John Palmieri for <p>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.</p>
<p>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:</p>
<pre><code>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()
</code></pre>
<p>However, I realised I'm picking Suits with reposition, which is not what I want!</p>
<p>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!</p>
<p>Any help would be really appreciated!</p>
https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?comment=69345#post-id-69345The 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).Wed, 21 Jun 2023 22:35:10 +0200https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?comment=69345#post-id-69345Comment by John Palmieri for <p>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.</p>
<p>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:</p>
<pre><code>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()
</code></pre>
<p>However, I realised I'm picking Suits with reposition, which is not what I want!</p>
<p>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!</p>
<p>Any help would be really appreciated!</p>
https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?comment=69343#post-id-69343Tutorial link: https://doc.sagemath.org/html/en/reference/combinat/sage/combinat/tutorial.htmlWed, 21 Jun 2023 22:30:13 +0200https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?comment=69343#post-id-69343Answer by John Palmieri for <p>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.</p>
<p>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:</p>
<pre><code>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()
</code></pre>
<p>However, I realised I'm picking Suits with reposition, which is not what I want!</p>
<p>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!</p>
<p>Any help would be really appreciated!</p>
https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?answer=69346#post-id-69346Using 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]
Wed, 21 Jun 2023 22:43:27 +0200https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?answer=69346#post-id-69346Comment by Luisz for <p>Using what you have so far:</p>
<pre><code>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
</code></pre>
<p>Now we can construct the hands in several ways. Using a loop:</p>
<pre><code>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)
</code></pre>
<p>Or more briefly using list comprehension:</p>
<pre><code>Hands = [[(a[0], s)] + [(a[1], Suits[i]) for i in range(4)] for a in X for s in Suits]
</code></pre>
https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?comment=69424#post-id-69424So, 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!Sat, 24 Jun 2023 11:00:39 +0200https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?comment=69424#post-id-69424Answer by Max Alekseyev for <p>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.</p>
<p>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:</p>
<pre><code>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()
</code></pre>
<p>However, I realised I'm picking Suits with reposition, which is not what I want!</p>
<p>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!</p>
<p>Any help would be really appreciated!</p>
https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?answer=69324#post-id-69324Here is a way:
FourOfaKind = [ {(v,s) for s in Suits} | {(v2,s2)} for v,v2 in Permutations(Values,2) for s2 in Suits ]Wed, 21 Jun 2023 14:39:21 +0200https://ask.sagemath.org/question/69313/making-card-configurations-in-sagemath/?answer=69324#post-id-69324