ListTools
Deal
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
Calling Sequence
Parameters
Description
Examples
Compatibility
Deal( Container, numhands, handsreturned )
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)].
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.
Simple Examples
Example 1
Example 2
Example 3
Example 4
Example 5
Example 6
Example 7
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:
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:
Second, assemble typeset versions of all the cards available for each suit:
Third, create a shuffled deck of the above cards:
Finally, deal 5 cards each to the four players:
Application 2
Consider the following string:
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:
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.
See Also
ListTools[Slice]
Statistics[Shuffle]
StringTools
StringTools[RegSplit]
Typesetting
Download Help Document