Tutorial: Basic combinatorics in Sage

<span id="tutorial-basic-combinatorics"></span>

<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name">
<col class="docinfo-content">
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Florent Hivert &lt;<a class="reference external" href="mailto:florent.hivert@univ-rouen.fr">florent.hivert&#64;univ-rouen.fr</a>&gt;</td></tr>
</tbody>
</table>

<h1>Exercice: Poker and probability</h1>
<p>A poker card is characterized by a <em>suit</em> (&quot;spades&quot;, &quot;hearts&quot;, &quot;diamonds&quot;,
&quot;clubs&quot;) and a <em>rank</em> ($2, 3, \dots, 9$, &quot;Jack&quot;, &quot;Queen&quot;, &quot;King&quot; and &quot;Ace&quot;).
Here is the way to define suits in sage:</p>

{{{id=0|
Suits = Set([&quot;spades&quot;, &quot;hearts&quot;, &quot;diamonds&quot;, &quot;clubs&quot;])
///
}}}

<p>Define similarly the set <tt class="docutils literal">Ranks</tt>:</p>

{{{id=1|

///
}}}

<p>The deck of card is the cartesian product of the set of suits by the set of
ranks. Define a set <tt class="docutils literal">Cards</tt> accordingly:</p>

{{{id=2|

///
}}}

<p>Use the method <tt class="docutils literal">.cardinality()</tt> to compute the number of suits, ranks and
cards:</p>

{{{id=3|

///
}}}


{{{id=4|

///
}}}


{{{id=5|

///
}}}

<p>Draw a card at random:</p>

{{{id=6|

///
}}}

<p>Cards are (currently) returned as lists. To be able to build a set of cards,
we need them to be <em>hashable</em>. Let's redefine the set of cards by transforming
cards to tuples:</p>

{{{id=7|
Cards = CartesianProduct(Suits, Ranks).map(tuple)
///
}}}

<p>Use <tt class="docutils literal">Subsets</tt> to draw a hand of five cards at random:</p>

{{{id=8|

///
}}}

<p>Use <tt class="docutils literal">.cardinality()</tt> to compute the number of hands, check the result with
<tt class="docutils literal">binomial</tt>:</p>

{{{id=9|

///
}}}

<p>To go further, see exercises 38, 39, 40 in <em>Calcul Mathématique avec Sage</em>
(version 1.0) page 255.</p>


<h1>Using existing Enumerated Sets</h1>
<ol class="arabic">
<li><p class="first">List all the strict partitions of $5$ (hint: use
<tt class="docutils literal">Partitions</tt> with <tt class="docutils literal">max_slope</tt>):</p>

{{{id=10|

///
}}}

</li>
<li><p class="first">List all the vectors of <tt class="docutils literal">0</tt> and <tt class="docutils literal">1</tt> of length <tt class="docutils literal">5</tt> (hint: use
<tt class="docutils literal">IntegerVectors</tt> with <tt class="docutils literal">max_part</tt>):</p>

{{{id=11|

///
}}}

<p>You can also use a cartesian product:</p>

{{{id=12|

///
}}}

</li>
<li><p class="first">List all the Dyck words of length <tt class="docutils literal">6</tt>:</p>

{{{id=13|

///
}}}

</li>
</ol>
<p>Here is the way to print the standard tableaux of size $4$:</p>

{{{id=14|
for t in StandardTableaux(3): t.pp(); print
///
1  2  31  321  23123
}}}

<ol class="arabic">
<li><p class="first">Define the set of all the partitions of $1$ to $5$ (hint: use
<tt class="docutils literal">DisjointUnionEnumeratedSets</tt>):</p>

{{{id=15|

///
}}}

</li>
</ol>




