Permutations: Difference between revisions

2,836 bytes removed ,  2 years ago
Undo revision 356566 by GordonCharlton (talk)
(→‎{{header|Quackery}}: second method)
(Undo revision 356566 by GordonCharlton (talk))
Line 7,290:
 
=={{header|Quackery}}==
===General Solution===
 
The word ''perms'' solves a more general task; generate permutations of between ''a'' and ''b'' items (inclusive) from the specified nest.
Line 7,362 ⟶ 7,361:
kucq kuca kaq kaqu kaqc kau kauq kauc kac kacq
kacu kcq kcqu kcqa kcu kcuq kcua kca kcaq kcau</pre>
 
===An Uncommon Ordering===
 
The central idea is that given a list of the permutations of say 3 items, each permutation can be used to generate 4 of the permutations of 4 items, so for example, from <code>[ 2 0 1 ]</code> we can generate
::<code>[ 3 2 0 1 ]</code>
::<code>[ 2 3 0 1 ]</code>
::<code>[ 2 0 3 1 ]</code>
::<code>[ 2 0 1 3 ]</code>
by stuffing the 3 into each of the 4 possible positions that it could go.
 
The code start with a nest of all the permutations of 0 items <code>[ [ ] ]</code>, and each time though the outer <code>times</code> loop (i.e. 4 times in the example) it takes each of the permutations generated so far (this is the <code>witheach</code> loop) and applies the central idea descried above (that is the inner <code>times</code> loop.)
 
'''Some aids to reading the code.'''
 
Quackery is a stack based language. If you are unfamiliar the with words <code>swap</code>, <code>rot</code>, <code>dup</code>, <code>2dup</code>, <code>dip</code>, <code>unrot</code> or <code>drop</code> they can be skimmed over as "noise" to get a gist of the process.
 
<code>[]</code> creates an empty nest <code>[ ]</code>.
 
<code>times</code> indicates that the word or nest following it is to be repeated a specified number of times. (The specified number is on the top of the stack, so <code>4 times [ ... ]</code>repeats some arbitrary code 4 times.
 
<code>i</code> returns the number of times a <code>times</code> loop has left to repeat. It counts down to zero.
 
<code>i^</code> returns the number of times a <code>times</code> loop has been repeated. It counts up from zero.
 
<code>size</code> returns the number of items (words, numbers, nests) in a nest.
 
<code>witheach</code> indicates that the word or nest following it is to be repeated once for each item in a specified nest, with successive items from the nest available on the top of stack on each repetition.
 
<code>999 ' [ 10 11 12 13 ] 3 stuff</code> will return <code>[ 10 11 12 99 13 ]</code>by stuffing the number 99 into the 3rd position in the nest. (The start of a nest is the zeroth position, the end of this nest is the 5th position.)
 
<code>nested join</code> adds a nest to the end of a nest as its last item.
 
=={{header|Quackery}}==
 
<lang Quackery> [ ' [ [ ] ] swap times
[ [] i rot witheach
[ dup size 1+ times
[ 2dup i^ stuff
dip rot nested join
unrot ] drop ] drop ] ] is perms ( n --> [ )
 
4 perms witheach [ echo cr ]
</lang>
 
{{out}}
 
<pre>[ 0 1 2 3 ]
[ 1 0 2 3 ]
[ 1 2 0 3 ]
[ 1 2 3 0 ]
[ 0 2 1 3 ]
[ 2 0 1 3 ]
[ 2 1 0 3 ]
[ 2 1 3 0 ]
[ 0 2 3 1 ]
[ 2 0 3 1 ]
[ 2 3 0 1 ]
[ 2 3 1 0 ]
[ 0 1 3 2 ]
[ 1 0 3 2 ]
[ 1 3 0 2 ]
[ 1 3 2 0 ]
[ 0 3 1 2 ]
[ 3 0 1 2 ]
[ 3 1 0 2 ]
[ 3 1 2 0 ]
[ 0 3 2 1 ]
[ 3 0 2 1 ]
[ 3 2 0 1 ]
[ 3 2 1 0 ]
</pre>
 
=={{header|R}}==
1,483

edits