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

Content added Content deleted
m (Fix Perl 6 -> Raku links)
(Added AppleScript.)
Line 57: Line 57:
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♥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♣
Finally create your own 51 digit factorial base number and produce the corresponding permutation of the above shoe
Finally create your own 51 digit factorial base number and produce the corresponding permutation of the above shoe

=={{header|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.

<lang applescript>-- Permutate a list according to a given factorial base number.
on FBNShuffle(|Ω|, fbn)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set fbnDigits to fbn's text items
set AppleScript's text item delimiters to astid
repeat with m from 1 to (count fbnDigits)
set m_plus_g to m + (item m of fbnDigits)
set v to item m_plus_g of |Ω|
repeat with i from (m_plus_g - 1) to m by -1
set item (i + 1) of |Ω| to item i of |Ω|
end repeat
set item m of |Ω| to v
end repeat
end FBNShuffle

-- Generate all the factorial base numbers having a given number of digits.
on generateFBNs(numberOfDigits)
script o
property partials : {}
property permutations : {}
end script
if (numberOfDigits > 0) then
repeat with i from 0 to numberOfDigits
set end of o's permutations to (i as text)
end repeat
repeat with maxDigit from (numberOfDigits - 1) to 1 by -1
set o's partials to o's permutations
set o's permutations to {}
repeat with i from 1 to (count o's partials)
set thisPartial to item i of o's partials
repeat with j from 0 to maxDigit
set end of o's permutations to (thisPartial & ("." & j))
end repeat
end repeat
end repeat
end if
return o's permutations
end generateFBNs

-- Generate a random factorial base number having a given number of digits.
on generateRandomFBN(numberOfDigits)
set fbnDigits to {}
repeat with maxDigit from numberOfDigits to 1 by -1
set end of fbnDigits to (random number maxDigit)
end repeat
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set fbn to fbnDigits as text
set AppleScript's text item delimiters to astid
return fbn
end generateRandomFBN

(* Test code *)

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""

-- 1. Reproduce table of {0, 1, 2, 3} permutations
set output to {"1. Reproduce table of {0, 1, 2, 3} permutations:"}
set elements to {0, 1, 2, 3}
set listOfFBNs to generateFBNs((count elements) - 1)
repeat with fbn in listOfFBNs
copy elements to |Ω|
FBNShuffle(|Ω|, fbn)
set end of output to fbn's contents & " -> " & |Ω|
end repeat

-- 2. Generate and count all 11-digit permutations. No way!
set end of output to ""
set numberOfDigits to 11
set numberOfPermutations to 1
repeat with base from 2 to (numberOfDigits + 1)
set numberOfPermutations to numberOfPermutations * base
end repeat
set end of output to "2. " & numberOfDigits & "-digit factorial base numbers have " & (numberOfPermutations div 1) & " possible permutations!"

-- 3. Card shoe permutations with the given FBNs.
set end of output to ""
set shoe to {"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♣"}
copy shoe to shoe1
copy shoe to shoe2
set fbn1 to "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"
set fbn2 to "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"
FBNShuffle(shoe1, fbn1)
FBNShuffle(shoe2, fbn2)
set end of output to "3. Shuffle " & shoe
set end of output to "With " & fbn1 & (linefeed & " -> " & shoe1)
set end of output to "With " & fbn2 & (linefeed & " -> " & shoe2)

-- 4. Card shoe permutation with randomly generated FBN.
set end of output to ""
set fbn3 to generateRandomFBN(51)
FBNShuffle(shoe, fbn3)
set end of output to "4. With randomly generated " & fbn3 & (linefeed & " -> " & shoe)

set AppleScript's text item delimiters to linefeed
set output to output as text
set AppleScript's text item delimiters to astid
return output</lang>

{{output}}
<lang applescript>"1. Reproduce table of {0, 1, 2, 3} permutations:
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

2. 11-digit factorial base numbers have 479001600 possible permutations!

3. Shuffle 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♣
With 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
-> 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♦
With 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
-> 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♠

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♦"</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==