|
Calling Sequence
|
|
Deal( Container, numhands, handsreturned )
|
|
Parameters
|
|
Container
|
-
|
any container (list, set, 1-D rtable), to be dealt into sub-containers
|
numhands
|
-
|
positive integer, specifying the target number of sub-containers
|
handsreturned
|
-
|
(optional) positive or negative integer, or list/set of positive or negative integers, specifying which hands are to be returned. The default is [seq(1..numhands)].
|
|
|
|
|
Description
|
|
•
|
The command deals (in a manner analogous to a deck of cards) the elements of a container into a sequence of sub-containers with the number of elements in each sub-container differing by at most one.
|
•
|
As the name of the command suggests, elements of the original container are dealt one-by-one to each sub-container in order, and this process is repeated until no more elements remain.
|
•
|
If the specified number of sub-containers is greater than the number of elements in the original container, then the result is a sequence of sub-containers consisting of single elements, followed by empty sub-containers.
|
•
|
When the optional parameter handsreturned is passed, only the specified sub-containers are computed and returned. Note that an error will be thrown if 0, or any number larger than numhands or smaller than -numhands, is passed.
|
|
|
Examples
|
|
|
Simple Examples
|
|
|
Example 1
|
|
>
|
|
| (1) |
| (2) |
|
|
Example 2
|
|
| (3) |
| (4) |
| (5) |
|
|
Example 3
|
|
|
|
Example 4
|
|
| (8) |
>
|
|
|
|
Example 5
|
|
>
|
|
| (10) |
| (11) |
>
|
|
|
|
Example 6
|
|
>
|
|
| (13) |
| (14) |
>
|
|
|
|
Example 7
|
|
•
|
The Deal command can be used to amend a flat list in order to initialize a Matrix:
|
| (16) |
|
|
|
Comparison with the Slice command
|
|
•
|
The Deal command differs from the Slice command in how the elements are ordered. Specifically, Slice keeps the elements in the same order. For example:
|
| (19) |
| (20) |
| (21) |
|
|
Application 1
|
|
•
|
Here, we will randomly construct a deck of cards (with typesetting!), and then deal five cards each to four players.
|
•
|
First, define a list for the card ranks:
|
>
|
|
| (22) |
•
|
Second, assemble typeset versions of all the cards available for each suit:
|
>
|
|
| (23) |
>
|
|
| (24) |
>
|
|
| (25) |
>
|
|
| (26) |
•
|
Third, create a shuffled deck of the above cards:
|
>
|
|
| (27) |
•
|
Finally, deal 5 cards each to the four players:
|
| (28) |
|
|
Application 2
|
|
•
|
Consider the following string:
|
>
|
|
| (29) |
•
|
Suppose we want to extract the years from this string. One way to accomplish this is to use regular expressions and split the string at the sub-strings that consist of four consecutive digits:
|
>
|
|
| (30) |
•
|
This list stores the strings between the matches (years) and, since keepsplits=true, the years as well. Since the years are stored in the elements with even indices:
|
>
|
|
| (31) |
|
|
|
Compatibility
|
|
•
|
The ListTools[Deal] command was introduced in Maple 2021.
|
|
|
|