Deal - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


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

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

withListTools:

randomize:

Simple Examples

Example 1

Aa,b,c,d,e

Aa,b,c,d,e

(1)

DealA,3

a,d,b,e,c

(2)

Example 2

Bseq1..10

B1,2,3,4,5,6,7,8,9,10

(3)

DealB,2

1,3,5,7,9,2,4,6,8,10

(4)

DealB,2,1

1,3,5,7,9

(5)

Example 3

Cx,y,z

Cx,y,z

(6)

DealC,5

x,y,z,,

(7)

Example 4

Lseq1..5

L1,2,3,4,5

(8)

DealL,3,1,3

1,4,3

(9)

Example 5

Sa,b,c,d,e,f

Sa,b,c,d,e,f

(10)

DealS,3

a,d,b,e,c,f

(11)

DealS,3,1,1

a,d,c,f

(12)

Example 6

RVector1..10,ii2

R149162536496481100

(13)

DealR,4

12581,436100,949,1664

(14)

DealR,4,2,1

436100,1664

(15)

Example 7

• 

The Deal command can be used to amend a flat list in order to initialize a Matrix:

Lseq1..12

L1,2,3,4,5,6,7,8,9,10,11,12

(16)

MatrixDealL,3

147102581136912

(17)

MatrixDealL,4

159261037114812

(18)

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:

Lseq1..5

L1,2,3,4,5

(19)

ADealL,3

A1,4,2,5,3

(20)

BSliceL,3

B1,2,3,4,5

(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:

Ranksseq2..10,J,Q,K,A

Ranks2,3,4,5,6,7,8,9,10,J,Q,K,A

(22)
• 

Second, assemble typeset versions of all the cards available for each suit:

ClubsmapzTypesetting:−mocatz,,mathcolor=Green,Ranks

Clubs2♣,3♣,4♣,5♣,6♣,7♣,8♣,9♣,10♣,J♣,Q♣,K♣,A♣

(23)

DiamondsmapzTypesetting:−mocatz,,mathcolor=Blue,Ranks

Diamonds2♦,3♦,4♦,5♦,6♦,7♦,8♦,9♦,10♦,J♦,Q♦,K♦,A♦

(24)

HeartsmapzTypesetting:−mocatz,,mathcolor=Red,Ranks

Hearts2♥,3♥,4♥,5♥,6♥,7♥,8♥,9♥,10♥,J♥,Q♥,K♥,A♥

(25)

SpadesmapzTypesetting:−mocatz,,mathcolor=Black,Ranks

Spades2♠,3♠,4♠,5♠,6♠,7♠,8♠,9♠,10♠,J♠,Q♠,K♠,A♠

(26)
• 

Third, create a shuffled deck of the above cards:

DeckStatistics:-ShuffleseqClubs,seqDiamonds,seqHearts,seqSpades

Deck8♦,2♦,5♥,9♠,7♥,10♣,Q♦,3♣,7♣,Q♠,6♠,10♠,K♠,6♣,Q♥,A♥,4♦,J♥,2♥,Q♣,9♣,10♦,7♦,A♦,8♠,5♦,K♣,2♠,3♥,K♦,6♦,J♣,9♦,4♥,A♠,4♠,5♣,K♥,J♦,3♦,9♥,A♣,4♣,J♠,6♥,3♠,8♣,8♥,10♥,7♠,2♣,5♠

(27)
• 

Finally, deal 5 cards each to the four players:

DealDeck..20,4

8♦,7♥,7♣,K♠,4♦,2♦,10♣,Q♠,6♣,J♥,5♥,Q♦,6♠,Q♥,2♥,9♠,3♣,10♠,A♥,Q♣

(28)

Application 2

• 

Consider the following string:

strI was born in the year 1950, graduated university in 1973, and retired in 2010.

strI was born in the year 1950, graduated university in 1973, and retired in 2010.

(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:

SplitsStringTools:-RegSplit[0-9]{4},str,keepsplits

SplitsI was born in the year ,1950,, graduated university in ,1973,, and retired in ,2010,.

(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:

YearsDealSplits,2,2

Years1950,1973,2010

(31)

Compatibility

• 

The ListTools[Deal] command was introduced in Maple 2021.

• 

For more information on Maple 2021 changes, see Updates in Maple 2021.

See Also

ListTools

ListTools[Slice]

Statistics[Shuffle]

StringTools

StringTools[RegSplit]

Typesetting