Factorial base numbers indexing permutations of a collection: Difference between revisions

→‎{{header|Perl 6}}: Add a Perl 6 example
(→‎{{header|Perl 6}}: Add a Perl 6 example)
Line 127:
</pre>
8GB of memory is insufficient for rc's perm task
 
=={{header|Perl 6}}==
{{works with|Rakudo|2018.11}}
Using my interpretation of the task instructions as shown on the [http://rosettacode.org/wiki/Talk:Factorial_base_numbers_indexing_permutations_of_a_collection#Mojibake_and_misspellings discussion page].
 
<lang perl6>sub postfix:<!> (Int $n) { (flat 1, [\*] 1..*)[$n] }
 
multi base (Int $n is copy, 'F', $length? is copy) {
constant @fact = [\*] 1 .. *;
my $i = $length // @fact.first: * > $n, :k;
my $f;
[ @fact[^$i].reverse.map: { ($n, $f) = $n.polymod($_); $f } ]
}
 
sub fpermute (@a is copy, *@f) { (^@f).map: { @a[$_ .. $_ + @f[$_]].=rotate(-1) }; @a }
 
put "Part 1: Generate table";
put $_.&base('F', 3), ' -> ', [0,1,2,3].&fpermute($_.&base('F', 3)).join for ^24;
 
put "\nPart 2: Compare 11! to 11! " ~ '¯\_(ツ)_/¯';
# This is kind of a weird request. Since we don't actually need to _generate_
# the permutations, only _count_ them: compare count of 11! vs count of 11!
put "11! === 11! : ", 11! === 11!;
 
put "\nPart 3: Generate the given task shuffles" ;
my \Ω = <A♠ K♠ Q♠ J♠ 10♠ 9♠ 8♠ 7♠ 6♠ 5♠ 4♠ 3♠ 2♠ A♥ K♥ Q♥ J♥ 10♥ 9♥ 8♥ 7♥ 6♥ 5♥ 4♥ 3♥ 2♥
A♦ K♦ Q♦ J♦ 10♦ 9♦ 8♦ 7♦ 6♦ 5♦ 4♦ 3♦ 2♦ A♣ K♣ Q♣ J♣ 10♣ 9♣ 8♣ 7♣ 6♣ 5♣ 4♣ 3♣ 2♣
>;
 
my @books = <
39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0
51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1
>;
 
put Ω[(^Ω).&fpermute($_.split: '.')].join for @books;
 
put "\nPart 4: Generate a random shuffle";
my @shoe = (+Ω … 2).map: { (^$_).pick };
put Ω[(^Ω).&fpermute(@shoe)].join;
 
put "\nSeems to me it would be easier to just say: Ω.pick(*).join";
put Ω.pick(*).join;</lang>
{{out}}
<pre>Part 1: Generate table
0 0 0 -> 0123
0 0 1 -> 0132
0 1 0 -> 0213
0 1 1 -> 0231
0 2 0 -> 0312
0 2 1 -> 0321
1 0 0 -> 1023
1 0 1 -> 1032
1 1 0 -> 1203
1 1 1 -> 1230
1 2 0 -> 1302
1 2 1 -> 1320
2 0 0 -> 2013
2 0 1 -> 2031
2 1 0 -> 2103
2 1 1 -> 2130
2 2 0 -> 2301
2 2 1 -> 2310
3 0 0 -> 3012
3 0 1 -> 3021
3 1 0 -> 3102
3 1 1 -> 3120
3 2 0 -> 3201
3 2 1 -> 3210
 
Part 2: Compare 11! to 11! ¯\_(ツ)_/¯
11! === 11! : True
 
Part 3: Generate the given task shuffles
A♣3♣7♠4♣10♦8♦Q♠K♥2♠10♠4♦7♣J♣5♥10♥10♣K♣2♣3♥5♦J♠6♠Q♣5♠K♠A♦3♦Q♥8♣6♦9♠8♠4♠9♥A♠6♥5♣2♦7♥8♥9♣6♣7♦A♥J♦Q♦9♦2♥3♠J♥4♥K♦
2♣5♣J♥4♥J♠A♠5♥A♣6♦Q♠9♣3♦Q♥J♣10♥K♣10♣5♦7♥10♦3♠8♥10♠7♠6♥5♠K♥4♦A♥4♣2♥9♦Q♣8♣7♦6♣3♥6♠7♣2♦J♦9♥A♦Q♦8♦4♠K♦K♠3♣2♠8♠9♠
 
Part 4: Generate a random shuffle
5♠A♥9♠7♥10♦9♦10♣6♥6♠K♠J♦6♦5♥8♦5♣2♠J♣7♠9♥2♣J♥5♦3♥10♥A♣A♦3♦J♠K♥4♥Q♣10♠Q♠2♦4♠A♠7♦Q♦9♣K♣6♣3♣2♥8♥4♣K♦7♣8♣4♦8♠Q♥3♠
 
Seems to me it would be easier to just say: Ω.pick(*).join
3♥2♠8♣7♠8♦A♣6♣7♥4♠5♦9♣10♣5♠6♦K♠4♦Q♣8♥5♥5♣7♣J♥A♦9♠J♦9♦8♠K♥6♥K♦4♥3♦10♠7♦Q♠2♦9♥A♠J♣10♥3♠K♣Q♥10♦A♥4♣6♠J♠3♣2♥Q♦2♣</pre>
10,339

edits