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

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
m (syntax highlighting fixup automation)
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.
 
<langsyntaxhighlight lang="applescript">-- Permutate a list according to a given factorial base number.
on FBNShuffle(|Ω|, fbn)
set astid to AppleScript's text item delimiters
Line 168:
set output to output as text
set AppleScript's text item delimiters to astid
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"1. Reproduce table of {0, 1, 2, 3} permutations:
0.0.0 -> 0123
0.0.1 -> 0132
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
-> 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♦"</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
;The Functıons:
<langsyntaxhighlight lang="fsharp">
// Factorial base numbers indexing permutations of a collection
// Nigel Galloway: December 7th., 2018
Line 221:
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}
</syntaxhighlight>
</lang>
 
;Re-create the table:
<langsyntaxhighlight lang="fsharp">
lN [|0;0;0|] |> Seq.iter (fun n->printfn "%A -> %A" n (lN2p n [|0;1;2;3|]));;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 256:
 
;Shuffles:
<langsyntaxhighlight 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♣";|]
//Random Shuffle
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 [|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}}
<pre>
Line 273:
 
;Comparıson wıth [[http://www.rosettacode.org/wiki/Permutations#F.23 Permutations(F#)]]:
<langsyntaxhighlight lang="fsharp">
let g=[|0..10|]
lC 10 |> Seq.map(fun n->lc2p n g) |> Seq.length
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 285:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs io kernel literals math math.factorials
math.parser math.ranges prettyprint qw random sequences
splitting ;
Line 345:
: main ( -- ) show-table show-shuffles show-random-shuffle ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 389:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 536:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 583:
Factoradic representation of integer numbers in canonical form (with trailing zero).
 
<langsyntaxhighlight lang="haskell">import Data.List (unfoldr, intercalate)
 
newtype Fact = Fact [Int]
Line 605:
fromFact (Fact ds) = foldr f 0 $ zip [1..] ds
where
f (b, d) r = r * b + fromIntegral d</langsyntaxhighlight>
 
<pre>λ> toFact 2021
Line 618:
Correspondence with permutations:
 
<langsyntaxhighlight lang="haskell">toPermutation :: Fact -> [Int]
toPermutation (Fact ds) = go (reverse ds) [0.. length ds - 1]
where
Line 628:
permute :: [a] -> [Int] -> [a]
permute s p = case splitAt (length s - length p) s of
(s1,s2) -> s1 ++ map (s2 !!) p</langsyntaxhighlight>
 
<pre>λ> toPermutation (fact [4,0,2,1,0])
Line 640:
 
Given tasks
<langsyntaxhighlight lang="haskell">task1 = do
putStrLn "number\tfactoradic\tpermutation"
mapM_ display [0..23]
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♣"</langsyntaxhighlight>
 
<pre>λ> task1
Line 780:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function makefactorialbased(N, makelist)
listlist = Vector{Vector{Int}}()
count = 0
Line 839:
 
factbasenums()
</langsyntaxhighlight>{{output}}<pre>
0.0.0 -> [0, 1, 2, 3]
0.0.1 -> [0, 1, 3, 2]
Line 879:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, math, random, sequtils, strutils, unicode
 
# Representation of a factorial base number with N digits.
Line 974:
for i in 0..2:
echo "– for ", fbns[i], ':'
echo " ", Cards.permutation(fbns[i]).join(" ")</langsyntaxhighlight>
 
{{out}}
Line 1,024:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,068:
say "\nGenerate a random shuffle";
say my $shoe = join '.', map { int rand($_) } reverse 0..$#omega;
say fpermute($shoe,@omega);</langsyntaxhighlight>
{{out}}
<pre>Generate table
Line 1,111:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,234:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
"""
 
Line 1,445:
f.write("</pre>\n")
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
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.)
 
<langsyntaxhighlight Quackerylang="quackery"> [ 1 swap times [ i 1+ * ] ] is ! ( n --> n )
[ dup 0 = iff [ drop 2 ] done
Line 1,575:
say '"lucentbiography" is permutation' cr
say '#' $ 'lucentbiography' $ 'uncopyrightable' permnum echo
say ' of "uncopyrightable".'</langsyntaxhighlight>
 
'''Output:'''
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].
 
<syntaxhighlight lang="raku" perl6line>sub postfix:<!> (Int $n) { (flat 1, [\*] 1..*)[$n] }
 
multi base (Int $n is copy, 'F', $length? is copy) {
Line 1,635:
 
put "\nSeems to me it would be easier to just say: Ω.pick(*).join";
put Ω.pick(*).join;</langsyntaxhighlight>
{{out}}
<pre>Part 1: Generate table
Line 1,687:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/math" for Int
import "/fmt" for Fmt
Line 1,789:
perms = mapToPerms.call([fbn51])
for (d in perms[0]) System.write(cards[d])
System.print()</langsyntaxhighlight>
 
{{out}}
Line 1,832:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn fpermute(omega,num){ // eg (0,1,2,3), (0,0,0)..(3,2,1)
omega=omega.copy(); // omega gonna be mutated
foreach m,g in ([0..].zip(num)){ if(g) omega.insert(m,omega.pop(m+g)) }
omega
}</langsyntaxhighlight>
 
{{out|Part 1, Generate permutation table}}
<langsyntaxhighlight 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()));
}</langsyntaxhighlight>
{{out}}
<pre style="height:35ex">
Line 1,871:
 
{{out|Part 3, Generate the given task shuffles}}
<langsyntaxhighlight lang="zkl">deck:=List();
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) }
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")
.apply(fcn(s){ s.split(".").apply("toInt") });
foreach book in (books){ println(fpermute(deck,book).concat("")); }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,886:
 
{{out|Part 4, Generate a random shuffle}}
<langsyntaxhighlight lang="zkl">r:=[52..2,-1].pump(List,(0).random);
println(r.concat("."),"\n",fpermute(deck,r).concat(""));</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits