Factorial base numbers indexing permutations of a collection: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, made p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 61: | Line 61: | ||
It's not clear from the description what part of the four subtasks "your function" is supposed to handle. It's also unclear whether "generate all permutaions of 11 digits" means "generate all 479,001,600 11-digit factorial base numbers" or "generate all permutations of an 11-integer array using the 39,916,800 10-digit factorial base numbers." However, both of the latter are out of the question with AppleScript. |
It's not clear from the description what part of the four subtasks "your function" is supposed to handle. It's also unclear whether "generate all permutaions of 11 digits" means "generate all 479,001,600 11-digit factorial base numbers" or "generate all permutations of an 11-integer array using the 39,916,800 10-digit factorial base numbers." However, both of the latter are out of the question with AppleScript. |
||
< |
<syntaxhighlight lang="applescript">-- Permutate a list according to a given factorial base number. |
||
on FBNShuffle(|Ω|, fbn) |
on FBNShuffle(|Ω|, fbn) |
||
set astid to AppleScript's text item delimiters |
set astid to AppleScript's text item delimiters |
||
Line 168: | Line 168: | ||
set output to output as text |
set output to output as text |
||
set AppleScript's text item delimiters to astid |
set AppleScript's text item delimiters to astid |
||
return output</ |
return output</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">"1. Reproduce table of {0, 1, 2, 3} permutations: |
||
0.0.0 -> 0123 |
0.0.0 -> 0123 |
||
0.0.1 -> 0132 |
0.0.1 -> 0132 |
||
Line 206: | Line 206: | ||
4. With randomly generated 46.27.4.19.47.40.26.27.13.32.37.14.37.20.9.15.33.13.16.29.14.11.14.6.8.4.5.13.4.4.14.15.6.17.15.4.5.12.3.0.7.10.7.1.2.1.5.0.2.2.1 |
4. With randomly generated 46.27.4.19.47.40.26.27.13.32.37.14.37.20.9.15.33.13.16.29.14.11.14.6.8.4.5.13.4.4.14.15.6.17.15.4.5.12.3.0.7.10.7.1.2.1.5.0.2.2.1 |
||
-> 7♣K♦10♠7♥2♣10♣J♦9♦K♥2♦8♣J♥5♣3♥4♠8♥6♣10♥4♥J♣6♥A♥2♥7♠3♠9♠6♠8♦8♠5♠4♦A♣9♥4♣Q♣2♠5♥K♣J♠A♠6♦3♣5♦Q♠A♦Q♥9♣K♠7♦3♦10♦Q♦"</ |
-> 7♣K♦10♠7♥2♣10♣J♦9♦K♥2♦8♣J♥5♣3♥4♠8♥6♣10♥4♥J♣6♥A♥2♥7♠3♠9♠6♠8♦8♠5♠4♦A♣9♥4♣Q♣2♠5♥K♣J♠A♠6♦3♣5♦Q♠A♦Q♥9♣K♠7♦3♦10♦Q♦"</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
;The Functıons: |
;The Functıons: |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Factorial base numbers indexing permutations of a collection |
// Factorial base numbers indexing permutations of a collection |
||
// Nigel Galloway: December 7th., 2018 |
// Nigel Galloway: December 7th., 2018 |
||
Line 221: | Line 221: | ||
let fN g=if n.[g]=Ω-g then n.[g]<-0; false else n.[g]<-n.[g]+1; true |
let fN g=if n.[g]=Ω-g then n.[g]<-0; false else n.[g]<-n.[g]+1; true |
||
seq{yield n; while [1..Ω]|>List.exists(fun g->fN (Ω-g)) do yield n} |
seq{yield n; while [1..Ω]|>List.exists(fun g->fN (Ω-g)) do yield n} |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Re-create the table: |
;Re-create the table: |
||
< |
<syntaxhighlight lang="fsharp"> |
||
lN [|0;0;0|] |> Seq.iter (fun n->printfn "%A -> %A" n (lN2p n [|0;1;2;3|]));; |
lN [|0;0;0|] |> Seq.iter (fun n->printfn "%A -> %A" n (lN2p n [|0;1;2;3|]));; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 256: | Line 256: | ||
;Shuffles: |
;Shuffles: |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let shoe==[|"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♣";|] |
let shoe==[|"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♣";|] |
||
//Random Shuffle |
//Random Shuffle |
||
Line 263: | Line 263: | ||
lN2p [|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|] shoe|>Array.iter (printf "%s ");printfn "" |
lN2p [|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|] shoe|>Array.iter (printf "%s ");printfn "" |
||
lN2p [|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|] shoe|>Array.iter (printf "%s ");printfn "" |
lN2p [|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|] shoe|>Array.iter (printf "%s ");printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 273: | Line 273: | ||
;Comparıson wıth [[http://www.rosettacode.org/wiki/Permutations#F.23 Permutations(F#)]]: |
;Comparıson wıth [[http://www.rosettacode.org/wiki/Permutations#F.23 Permutations(F#)]]: |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let g=[|0..10|] |
let g=[|0..10|] |
||
lC 10 |> Seq.map(fun n->lc2p n g) |> Seq.length |
lC 10 |> Seq.map(fun n->lc2p n g) |> Seq.length |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 285: | Line 285: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: assocs io kernel literals math math.factorials |
||
math.parser math.ranges prettyprint qw random sequences |
math.parser math.ranges prettyprint qw random sequences |
||
splitting ; |
splitting ; |
||
Line 345: | Line 345: | ||
: main ( -- ) show-table show-shuffles show-random-shuffle ; |
: main ( -- ) show-table show-shuffles show-random-shuffle ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 389: | Line 389: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 536: | Line 536: | ||
} |
} |
||
fmt.Println() |
fmt.Println() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 583: | Line 583: | ||
Factoradic representation of integer numbers in canonical form (with trailing zero). |
Factoradic representation of integer numbers in canonical form (with trailing zero). |
||
< |
<syntaxhighlight lang="haskell">import Data.List (unfoldr, intercalate) |
||
newtype Fact = Fact [Int] |
newtype Fact = Fact [Int] |
||
Line 605: | Line 605: | ||
fromFact (Fact ds) = foldr f 0 $ zip [1..] ds |
fromFact (Fact ds) = foldr f 0 $ zip [1..] ds |
||
where |
where |
||
f (b, d) r = r * b + fromIntegral d</ |
f (b, d) r = r * b + fromIntegral d</syntaxhighlight> |
||
<pre>λ> toFact 2021 |
<pre>λ> toFact 2021 |
||
Line 618: | Line 618: | ||
Correspondence with permutations: |
Correspondence with permutations: |
||
< |
<syntaxhighlight lang="haskell">toPermutation :: Fact -> [Int] |
||
toPermutation (Fact ds) = go (reverse ds) [0.. length ds - 1] |
toPermutation (Fact ds) = go (reverse ds) [0.. length ds - 1] |
||
where |
where |
||
Line 628: | Line 628: | ||
permute :: [a] -> [Int] -> [a] |
permute :: [a] -> [Int] -> [a] |
||
permute s p = case splitAt (length s - length p) s of |
permute s p = case splitAt (length s - length p) s of |
||
(s1,s2) -> s1 ++ map (s2 !!) p</ |
(s1,s2) -> s1 ++ map (s2 !!) p</syntaxhighlight> |
||
<pre>λ> toPermutation (fact [4,0,2,1,0]) |
<pre>λ> toPermutation (fact [4,0,2,1,0]) |
||
Line 640: | Line 640: | ||
Given tasks |
Given tasks |
||
< |
<syntaxhighlight lang="haskell">task1 = do |
||
putStrLn "number\tfactoradic\tpermutation" |
putStrLn "number\tfactoradic\tpermutation" |
||
mapM_ display [0..23] |
mapM_ display [0..23] |
||
Line 676: | Line 676: | ||
\ 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♦\ |
||
\ 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♣"</syntaxhighlight> |
||
<pre>λ> task1 |
<pre>λ> task1 |
||
Line 780: | Line 780: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">function makefactorialbased(N, makelist) |
||
listlist = Vector{Vector{Int}}() |
listlist = Vector{Vector{Int}}() |
||
count = 0 |
count = 0 |
||
Line 839: | Line 839: | ||
factbasenums() |
factbasenums() |
||
</ |
</syntaxhighlight>{{output}}<pre> |
||
0.0.0 -> [0, 1, 2, 3] |
0.0.0 -> [0, 1, 2, 3] |
||
0.0.1 -> [0, 1, 3, 2] |
0.0.1 -> [0, 1, 3, 2] |
||
Line 879: | Line 879: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import algorithm, math, random, sequtils, strutils, unicode |
||
# Representation of a factorial base number with N digits. |
# Representation of a factorial base number with N digits. |
||
Line 974: | Line 974: | ||
for i in 0..2: |
for i in 0..2: |
||
echo "– for ", fbns[i], ':' |
echo "– for ", fbns[i], ':' |
||
echo " ", Cards.permutation(fbns[i]).join(" ")</ |
echo " ", Cards.permutation(fbns[i]).join(" ")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,024: | Line 1,024: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,068: | Line 1,068: | ||
say "\nGenerate a random shuffle"; |
say "\nGenerate a random shuffle"; |
||
say my $shoe = join '.', map { int rand($_) } reverse 0..$#omega; |
say my $shoe = join '.', map { int rand($_) } reverse 0..$#omega; |
||
say fpermute($shoe,@omega);</ |
say fpermute($shoe,@omega);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Generate table |
<pre>Generate table |
||
Line 1,111: | Line 1,111: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">fperm</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">fbn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">omega</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">fperm</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">fbn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">omega</span><span style="color: #0000FF;">)</span> |
||
Line 1,186: | Line 1,186: | ||
<span style="color: #000000;">show_cards</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fperm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fbn51s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">52</span><span style="color: #0000FF;">)))</span> |
<span style="color: #000000;">show_cards</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fperm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fbn51s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">52</span><span style="color: #0000FF;">)))</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,234: | Line 1,234: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python"> |
||
""" |
""" |
||
Line 1,445: | Line 1,445: | ||
f.write("</pre>\n") |
f.write("</pre>\n") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,534: | Line 1,534: | ||
(I have set my own tasks to illustrate the use of factorial base numbers with permutations as I do not find the assigned tasks particularly illuminating IMHO.) |
(I have set my own tasks to illustrate the use of factorial base numbers with permutations as I do not find the assigned tasks particularly illuminating IMHO.) |
||
< |
<syntaxhighlight lang="quackery"> [ 1 swap times [ i 1+ * ] ] is ! ( n --> n ) |
||
[ dup 0 = iff [ drop 2 ] done |
[ dup 0 = iff [ drop 2 ] done |
||
Line 1,575: | Line 1,575: | ||
say '"lucentbiography" is permutation' cr |
say '"lucentbiography" is permutation' cr |
||
say '#' $ 'lucentbiography' $ 'uncopyrightable' permnum echo |
say '#' $ 'lucentbiography' $ 'uncopyrightable' permnum echo |
||
say ' of "uncopyrightable".'</ |
say ' of "uncopyrightable".'</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
Line 1,595: | Line 1,595: | ||
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]. |
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 |
<syntaxhighlight lang="raku" line>sub postfix:<!> (Int $n) { (flat 1, [\*] 1..*)[$n] } |
||
multi base (Int $n is copy, 'F', $length? is copy) { |
multi base (Int $n is copy, 'F', $length? is copy) { |
||
Line 1,635: | Line 1,635: | ||
put "\nSeems to me it would be easier to just say: Ω.pick(*).join"; |
put "\nSeems to me it would be easier to just say: Ω.pick(*).join"; |
||
put Ω.pick(*).join;</ |
put Ω.pick(*).join;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Part 1: Generate table |
<pre>Part 1: Generate table |
||
Line 1,687: | Line 1,687: | ||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "/math" for Int |
import "/math" for Int |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 1,789: | Line 1,789: | ||
perms = mapToPerms.call([fbn51]) |
perms = mapToPerms.call([fbn51]) |
||
for (d in perms[0]) System.write(cards[d]) |
for (d in perms[0]) System.write(cards[d]) |
||
System.print()</ |
System.print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,832: | Line 1,832: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn fpermute(omega,num){ // eg (0,1,2,3), (0,0,0)..(3,2,1) |
||
omega=omega.copy(); // omega gonna be mutated |
omega=omega.copy(); // omega gonna be mutated |
||
foreach m,g in ([0..].zip(num)){ if(g) omega.insert(m,omega.pop(m+g)) } |
foreach m,g in ([0..].zip(num)){ if(g) omega.insert(m,omega.pop(m+g)) } |
||
omega |
omega |
||
}</ |
}</syntaxhighlight> |
||
{{out|Part 1, Generate permutation table}} |
{{out|Part 1, Generate permutation table}} |
||
< |
<syntaxhighlight lang="zkl">foreach a,b,c in (4,3,2){ |
||
println("%d.%d.%d --> %s".fmt(a,b,c, fpermute(T(0,1,2,3),T(a,b,c)).concat())); |
println("%d.%d.%d --> %s".fmt(a,b,c, fpermute(T(0,1,2,3),T(a,b,c)).concat())); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:35ex"> |
<pre style="height:35ex"> |
||
Line 1,871: | Line 1,871: | ||
{{out|Part 3, Generate the given task shuffles}} |
{{out|Part 3, Generate the given task shuffles}} |
||
< |
<syntaxhighlight lang="zkl">deck:=List(); |
||
foreach s,c in ("\u2660 \u2665 \u2666 \u2663".split(), |
foreach s,c in ("\u2660 \u2665 \u2666 \u2663".split(), |
||
"A K Q J 10 9 8 7 6 5 4 3 2".split()){ deck.append(c+s) } |
"A K Q J 10 9 8 7 6 5 4 3 2".split()){ deck.append(c+s) } |
||
Line 1,878: | Line 1,878: | ||
"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") |
"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") |
||
.apply(fcn(s){ s.split(".").apply("toInt") }); |
.apply(fcn(s){ s.split(".").apply("toInt") }); |
||
foreach book in (books){ println(fpermute(deck,book).concat("")); }</ |
foreach book in (books){ println(fpermute(deck,book).concat("")); }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,886: | Line 1,886: | ||
{{out|Part 4, Generate a random shuffle}} |
{{out|Part 4, Generate a random shuffle}} |
||
< |
<syntaxhighlight lang="zkl">r:=[52..2,-1].pump(List,(0).random); |
||
println(r.concat("."),"\n",fpermute(deck,r).concat(""));</ |
println(r.concat("."),"\n",fpermute(deck,r).concat(""));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |