Subscribe Bookmark RSS Feed

Permutations Function

I've been looking through the JMP 8 manual for a function that returns every possible permutation of all the integers from 1 to N, but haven't been able to find one. I can write one easily enough, but I don't want to bother if there's one already available that I've overlooked. Is there one?
7 REPLIES
mpb

Super User

Joined:

Jun 23, 2011

Can the "col shuffle" function do what you need?
Unfortunately no: I need to look at every permutation systematically. As I said, I can write a function that will do it easily enough; I'm just a little surprised that there doesn't already appear to be one. I'd have thought that it would be a useful addition to the armoury.

Many thanks
ms

Super User

Joined:

Jun 23, 2011

You can achieve that with the "Cartesian Join" option in the Join platform. Create two tables with one column each and n rows. Join the second with the first table, and then rejoin the output with first n-1 times. I tried with n=3 and got a table with 27 rows containing every permutation (distributed over 3 columns).

Would be easy to script in a for loop (use invisible option for large n!), but that was maybe what you already had in mind.
Ingenious! But that's going to generate 27 (3x3x3) lists, i.e. {1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 2, 1}, ... , {3, 3, 3}, isn't it, whereas what I'd actually be after in such a situation would be just the following 6 (3x2x1) lists:

{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2} and {3, 2, 1}

I could eliminate any of the lists generated from the cartesian join with duplicate integers easily enough, but there ought to be a more efficient way than that - maybe using a recursive function. If I think one up I'll post it unless anyone beats me to it.

Many thanks!
I've found one that was originally written in C++ and just translated it into JSL as shown below. The original can be found, with some others, at www.bearcave.com/random_hacks/permute.html


style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: green;">/*
Translated from the University
of Exeter algorithm */

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: black;">permute

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">function

style="font-size: 9pt; font-family: "Courier New"; color: black;">({

style="font-size: 9pt; font-family: "Courier New"; color: black;">v

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
start}

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
{default
local}

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
n

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">nitems

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">v)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">if

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">start

style="font-size: 9pt; font-family: "Courier New"; color: navy;">==

style="font-size: 9pt; font-family: "Courier New"; color: black;">n

style="font-size: 9pt; font-family: "Courier New"; color: navy;">-

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">print

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">v)





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">for

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">start

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
i

style="font-size: 9pt; font-family: "Courier New"; color: navy;"><

style="font-size: 9pt; font-family: "Courier New"; color: black;">n

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">++,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">                    
tmp

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
v[i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">+

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">]

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">
v[i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">+

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">]

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
v[start

style="font-size: 9pt; font-family: "Courier New"; color: navy;">+

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">]

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">
v[start

style="font-size: 9pt; font-family: "Courier New"; color: navy;">+

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">]

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
tmp

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">                    
permute(v

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
start

style="font-size: 9pt; font-family: "Courier New"; color: navy;">+

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">                    
v[start

style="font-size: 9pt; font-family: "Courier New"; color: navy;">+

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">]

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
v[i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">+

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">]

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">
v[i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">+

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">]

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
tmp





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">                    
)





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             
)





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: green;">/*
Try it out on some lists */

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">clear
log

style="font-size: 9pt; font-family: "Courier New"; color: black;">()

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">print

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"Example
1
(there should be 4! = 24 perms)"

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">v

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
{

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">2

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">3

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">4

style="font-size: 9pt; font-family: "Courier New"; color: black;">}

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">permute(v

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">0

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">print

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"Example
2
(there should be 6! = 720 perms"

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">v

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
{

style="font-size: 9pt; font-family: "Courier New"; color: teal;">4

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">2

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">6

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">3

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">5

style="font-size: 9pt; font-family: "Courier New"; color: black;">}

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">permute(v

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">0

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">print

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"Example
3
(there should be 5! = 120 perms"

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">v

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
{

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"Albert"

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"Bessie"

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"Carrie"

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"David"

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"Edgar"

style="font-size: 9pt; font-family: "Courier New"; color: black;">}

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">permute(v

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">0

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 




 

As a postscript, after finding the above I started looking on the internet for a similar recursive function to evaluate all the combinations of k items from N, but realised that it's a lot easier if you just create a multiple loop of the necessary depth in text and then parse it. The following function does that: to see the text that's being parsed, just uncomment the last line but two:


style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: black;">All_N_Choose_k

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">function

style="font-size: 9pt; font-family: "Courier New"; color: black;">({

style="font-size: 9pt; font-family: "Courier New"; color: black;">N

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
k}

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
{default
local}

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
c

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">repeat

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: teal;">0

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
k)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">
icount

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">0

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
answer

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">matrix

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">N
Choose k

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">N

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
k)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
k)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">for

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
i

style="font-size: 9pt; font-family: "Courier New"; color: navy;"><=

style="font-size: 9pt; font-family: "Courier New"; color: black;">k

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">++,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">if

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">==

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">                    
TextToParse

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"





style="font-size: 9pt; font-family: "Courier New"; color: purple;">
style="">                    
for(c[1]=1,
c[1]<=N-"

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">char

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">k

style="font-size: 9pt; font-family: "Courier New"; color: navy;">-

style="font-size: 9pt; font-family: "Courier New"; color: black;">i)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">",
c[1]++,"

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">                    

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">                    
TextToParse

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
TextToParse

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"





style="font-size: 9pt; font-family: "Courier New"; color: purple;">
style="">                    
for(c["

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">char

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">i)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"]=c["

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">char

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">-

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"]+1,
c["

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">                    

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">char

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">i)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"]<=N-"

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">char

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">k

style="font-size: 9pt; font-family: "Courier New"; color: navy;">-

style="font-size: 9pt; font-family: "Courier New"; color: black;">i)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">",
c["

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">char

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">i)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"]++,"

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">                    
)





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">             
)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
TextToParse

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
TextToParse

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">"





style="font-size: 9pt; font-family: "Courier New"; color: purple;">
style="">      
icount++;
for(i=1, i<=k, i++, answer[icount, i] = c[ i ])"

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">for

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: teal;">1

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
i

style="font-size: 9pt; font-family: "Courier New"; color: navy;"><=

style="font-size: 9pt; font-family: "Courier New"; color: black;">k

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
i

style="font-size: 9pt; font-family: "Courier New"; color: navy;">++,

style="font-size: 9pt; font-family: "Courier New"; color: black;">
TextToParse

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
TextToParse

style="font-size: 9pt; font-family: "Courier New"; color: navy;">||

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: purple;">")"

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: green;">//
style="">    
show(TextToParse);

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">eval

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">parse

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">TextToParse))

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
answer





style="font-size: 9pt; font-family: "Courier New"; color: black;">
style="">      
)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: black;">W

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
All_N_Choose_k(

style="font-size: 9pt; font-family: "Courier New"; color: teal;">10

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">3

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">
dtw

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">as
table

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">W)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">X

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
All_N_Choose_k(

style="font-size: 9pt; font-family: "Courier New"; color: teal;">15

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">5

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">
dtx

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">as
table

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">X)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">Y

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
All_N_Choose_k(

style="font-size: 9pt; font-family: "Courier New"; color: teal;">20

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">5

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">
dty

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">as
table

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">Y)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">Z

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">
All_N_Choose_k(

style="font-size: 9pt; font-family: "Courier New"; color: teal;">20

style="font-size: 9pt; font-family: "Courier New"; color: navy;">,

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: teal;">6

style="font-size: 9pt; font-family: "Courier New"; color: black;">)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">
dtz

style="font-size: 9pt; font-family: "Courier New"; color: navy;">=

style="font-size: 9pt; font-family: "Courier New"; color: black;">

style="font-size: 9pt; font-family: "Courier New"; color: rgb(0, 0, 221);">as
table

style="font-size: 9pt; font-family: "Courier New"; color: black;">(

style="font-size: 9pt; font-family: "Courier New"; color: black;">Z)

style="font-size: 9pt; font-family: "Courier New"; color: navy;">;

style="font-size: 9pt; font-family: "Courier New"; color: black;">





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 





style="font-size: 9pt; font-family: "Courier New"; color: black;">

 




 






style="font-size: 9pt; line-height: 115%; font-family: "Courier New"; color: navy;">



The above also illustrates just how fast the calculations are being done (on my machine it's less than one second), bearing in mind that the last of the four tables created by the examples at the bottom contains 20! / ((6!) x (14!)) = 38,760 rows.
ms

Super User

Joined:

Jun 23, 2011

Thanks for sharing these functions.

You're right about that mine suggestion was giving the wrong result. The Cartesian Join does not yield permutations. It's more like all the possible outcomes in an old urn problem: draw n balls with replacement after each draw (I come think of the multinomial distribution beeing related to this..)