Factorial base numbers indexing permutations of a collection: Difference between revisions
Content deleted Content added
→{{header|Quackery}}: added figits |
Added Wren |
||
Line 1,532: | Line 1,532: | ||
Seems to me it would be easier to just say: Ω.pick(*).join |
Seems to me it would be easier to just say: Ω.pick(*).join |
||
5♦3♠8♦10♦2♥7♠7♦Q♦A♠5♣8♣Q♠4♠2♦K♦5♠Q♥7♣10♠2♠K♠J♣9♣3♣4♥3♥4♦3♦Q♣2♣4♣J♦9♠A♣J♠10♣6♣9♦6♠10♥6♥9♥J♥7♥K♥A♦8♠A♥5♥8♥K♣6♦</pre> |
5♦3♠8♦10♦2♥7♠7♦Q♦A♠5♣8♣Q♠4♠2♦K♦5♠Q♥7♣10♠2♠K♠J♣9♣3♣4♥3♥4♦3♦Q♣2♣4♣J♦9♠A♣J♠10♣6♣9♦6♠10♥6♥9♥J♥7♥K♥A♦8♠A♥5♥8♥K♣6♦</pre> |
||
=={{header|Wren}}== |
|||
{{trans|Go}} |
|||
{{libheader|Wren-math}} |
|||
{{libheader|Wren-fmt}} |
|||
<lang ecmascript>import "random" for Random |
|||
import "/math" for Int |
|||
import "/fmt" for Fmt |
|||
var genFactBaseNums = Fn.new { |size, countOnly| |
|||
var results = [] |
|||
var count = 0 |
|||
var n = 0 |
|||
while (true) { |
|||
var radix = 2 |
|||
var res = null |
|||
if (!countOnly) res = List.filled(size, 0) |
|||
var k = n |
|||
while (k > 0) { |
|||
var div = (k/radix).floor |
|||
var rem = k % radix |
|||
if (!countOnly) { |
|||
if (radix <= size + 1) res[size-radix+1] = rem |
|||
} |
|||
k = div |
|||
radix = radix + 1 |
|||
} |
|||
if (radix > size+2) break |
|||
count = count + 1 |
|||
if (!countOnly) results.add(res) |
|||
n = n + 1 |
|||
} |
|||
return [results, count] |
|||
} |
|||
var mapToPerms = Fn.new { |factNums| |
|||
var perms = [] |
|||
var psize = factNums[0].count + 1 |
|||
var start = List.filled(psize, 0) |
|||
for (i in 0...psize) start[i] = i |
|||
for (fn in factNums) { |
|||
var perm = start.toList |
|||
for (m in 0...fn.count) { |
|||
var g = fn[m] |
|||
if (g != 0) { |
|||
var first = m |
|||
var last = m + g |
|||
for (i in 1..g) { |
|||
var temp = perm[first] |
|||
for (j in first+1..last) perm[j-1] = perm[j] |
|||
perm[last] = temp |
|||
} |
|||
} |
|||
} |
|||
perms.add(perm) |
|||
} |
|||
return perms |
|||
} |
|||
var join = Fn.new { |ints, sep| ints.map { |i| i.toString }.join(sep) } |
|||
var undot = Fn.new { |s| s.split(".").map { |ss| Num.fromString(ss) }.toList } |
|||
var rand = Random.new() |
|||
// Recreate the table. |
|||
var factNums = genFactBaseNums.call(3, false)[0] |
|||
var perms = mapToPerms.call(factNums) |
|||
var i = 0 |
|||
for (fn in factNums) { |
|||
Fmt.print("$s -> $s", join.call(fn, "."), join.call(perms[i], "")) |
|||
i = i + 1 |
|||
} |
|||
// Check that the number of perms generated is equal to 11! (this takes a while). |
|||
var count = genFactBaseNums.call(10, true)[1] |
|||
Fmt.print("\nPermutations generated = $,d", count) |
|||
Fmt.print("compared to 11! which = $,d", Int.factorial(11)) |
|||
System.print() |
|||
// Generate shuffles for the 2 given 51 digit factorial base numbers. |
|||
var fbn51s = [ |
|||
"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" |
|||
] |
|||
factNums = [undot.call(fbn51s[0]), undot.call(fbn51s[1])] |
|||
perms = mapToPerms.call(factNums) |
|||
var shoe = "A♠K♠Q♠J♠T♠9♠8♠7♠6♠5♠4♠3♠2♠A♥K♥Q♥J♥T♥9♥8♥7♥6♥5♥4♥3♥2♥A♦K♦Q♦J♦T♦9♦8♦7♦6♦5♦4♦3♦2♦A♣K♣Q♣J♣T♣9♣8♣7♣6♣5♣4♣3♣2♣".toList |
|||
var cards = List.filled(52, null) |
|||
for (i in 0..51) { |
|||
cards[i] = shoe[2*i..2*i+1].join() |
|||
if (cards[i][0] == "T") cards[i] = "10" + cards[i][1..-1] |
|||
} |
|||
i = 0 |
|||
for (fbn51 in fbn51s) { |
|||
System.print(fbn51) |
|||
for (d in perms[i]) System.write(cards[d]) |
|||
System.print("\n") |
|||
i = i + 1 |
|||
} |
|||
// Create a random 51 digit factorial base number and produce a shuffle from that. |
|||
var fbn51 = List.filled(51, 0) |
|||
for (i in 0..50) fbn51[i] = rand.int(52-i) |
|||
System.print(join.call(fbn51, ".")) |
|||
perms = mapToPerms.call([fbn51]) |
|||
for (d in perms[0]) System.write(cards[d]) |
|||
System.print()</lang> |
|||
{{out}} |
|||
<pre> |
|||
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 |
|||
Permutations generated = 39,916,800 |
|||
compared to 11! which = 39,916,800 |
|||
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♦ |
|||
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♠ |
|||
35.12.47.3.41.40.22.9.38.8.26.15.29.23.23.0.17.19.32.5.31.10.8.26.1.18.15.6.23.21.9.13.13.1.7.11.1.1.2.5.8.6.4.1.2.3.3.2.1.2.0 |
|||
5♦2♠4♣J♠9♣10♣3♥4♠8♣5♠9♦8♥3♦J♦10♦A♠4♥K♦3♣7♠2♣J♥K♥7♣Q♠6♦Q♦A♥5♣J♣7♥8♦7♦10♠9♥4♦9♠8♠3♠5♥A♣A♦6♥6♠10♥2♦K♣2♥Q♥6♣K♠Q♣ |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |