deal the elements of a 1-D container into a sequence of sub-containers with the number of elements in each sub-container differing by at most one
Deal( Container, numhands, handsreturned )
any container (list, set, 1-D rtable), to be dealt into sub-containers
positive integer, specifying the target number of sub-containers
(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)].
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.
The Deal command can be used to amend a flat list in order to initialize a Matrix:
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:
Here, we will randomly construct a deck of cards consisting of 20 cards (for simplicity), and then deal five cards each to four players.
First, define lists for the card values and suits:
Second, using random number generators, create a unique deck of 20 cards:
Finally, deal 5 cards each to the four players:
Consider the following string:
str≔I was born in the year 1950, graduated university in 1973, and retired in 2010.
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:
Splits≔I was born in the year ,1950,, graduated university in ,1973,, and retired in ,2010,.
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:
The ListTools[Deal] command was introduced in Maple 2021.
For more information on Maple 2021 changes, see Updates in Maple 2021.
Download Help Document
What kind of issue would you like to report? (Optional)