Intersecting number wheels: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: simplified some code, added/changed whitespace and comments, made functions more idiomatic.) |
(Added Wren) |
||
Line 2,080: | Line 2,080: | ||
1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6 |
1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6 |
||
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4</pre> |
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4</pre> |
||
=={{header|Wren}}== |
|||
{{trans|Go}} |
|||
{{libheader|Wren-dynamic}} |
|||
{{libheader|Wren-sort}} |
|||
{{libheader|Wren-fmt}} |
|||
<lang ecmascript>import "/dynamic" for Struct |
|||
import "/sort" for Sort |
|||
import "/fmt" for Fmt |
|||
var Wheel = Struct.create("Wheel", ["next", "values"]) |
|||
var generate = Fn.new { |wheels, start, maxCount| |
|||
var count = 0 |
|||
var w = wheels[start] |
|||
while (true) { |
|||
var s = w.values[w.next] |
|||
var v = Num.fromString(s) |
|||
w.next = (w.next + 1) % w.values.count |
|||
wheels[start] = w |
|||
if (v) { |
|||
System.write("%(v) ") |
|||
count = count + 1 |
|||
if (count == maxCount) { |
|||
System.print("...\n") |
|||
return |
|||
} |
|||
} else { |
|||
while (true) { |
|||
var w2 = wheels[s] |
|||
var ss = s |
|||
s = w2.values[w2.next] |
|||
w2.next = (w2.next + 1) % w2.values.count |
|||
wheels[ss] = w2 |
|||
v = Num.fromString(s) |
|||
if (v) { |
|||
System.write("%(v) ") |
|||
count = count + 1 |
|||
if (count == maxCount) { |
|||
System.print("...\n") |
|||
return |
|||
} |
|||
break |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
var printWheels = Fn.new { |wheels| |
|||
var names = [] |
|||
for (name in wheels.keys) names.add(name) |
|||
Sort.quick(names) |
|||
System.print("Intersecting Number Wheel group:") |
|||
for (name in names) { |
|||
Fmt.print(" $s: $n", name, wheels[name].values) |
|||
} |
|||
System.write(" Generates:\n ") |
|||
} |
|||
var wheelMaps = [ |
|||
{ |
|||
"A": Wheel.new(0, ["1", "2", "3"]) |
|||
}, |
|||
{ |
|||
"A": Wheel.new(0, ["1", "B", "2"]), |
|||
"B": Wheel.new(0, ["3", "4"]) |
|||
}, |
|||
{ |
|||
"A": Wheel.new(0, ["1", "D", "D"]), |
|||
"D": Wheel.new(0, ["6", "7", "8"]) |
|||
}, |
|||
{ |
|||
"A": Wheel.new(0, ["1", "B", "C"]), |
|||
"B": Wheel.new(0, ["3", "4"]), |
|||
"C": Wheel.new(0, ["5", "B"]) |
|||
} |
|||
] |
|||
for (wheels in wheelMaps) { |
|||
printWheels.call(wheels) |
|||
generate.call(wheels, "A", 20) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Intersecting Number Wheel group: |
|||
A: [1, 2, 3] |
|||
Generates: |
|||
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 ... |
|||
Intersecting Number Wheel group: |
|||
A: [1, B, 2] |
|||
B: [3, 4] |
|||
Generates: |
|||
1 3 2 1 4 2 1 3 2 1 4 2 1 3 2 1 4 2 1 3 ... |
|||
Intersecting Number Wheel group: |
|||
A: [1, D, D] |
|||
D: [6, 7, 8] |
|||
Generates: |
|||
1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6 ... |
|||
Intersecting Number Wheel group: |
|||
A: [1, B, C] |
|||
B: [3, 4] |
|||
C: [5, B] |
|||
Generates: |
|||
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4 ... |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |