Intersecting number wheels: Difference between revisions
→{{header|jq}}: tidy
m (J) |
(→{{header|jq}}: tidy) |
||
(7 intermediate revisions by 5 users not shown) | |||
Line 65:
{{trans|Python}}
<
L
V nxt = w[name][0]
Line 86:
first = I first == ‘’ {name[0 .< (len)-1]} E first
V gen = (0.<20).map(i -> nextfrom(&@wheel, @first)).join(‘ ’)
print(" Generates:\n "gen" ...\n")</
{{out}}
Line 112:
=={{header|ALGOL 68}}==
<
# a number wheel element #
MODE NWELEMENT = UNION( CHAR # wheel name #, INT # wheel value # );
Line 167:
, NW( "B", LOC INT := 1, ( 3, 4 ) )
, NW( "C", LOC INT := 1, ( 5, "B" ) ) ) )
END</
{{out}}
<pre>
Line 193:
=={{header|AutoHotkey}}==
<
obj2 := {"A":[1, "B", 2] , "B":[3, 4]}
obj3 := {"A":[1, "D", "D"] , "D":[6, 7, 8]}
Line 234:
}
return [key, result]
}</
{{out}}
<pre>{A : 1,2,3}
Line 254:
=={{header|C}}==
<
#include <stdlib.h>
#include <string.h>
Line 394:
return 0;
}</
{{out}}
<pre> 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2
Line 402:
=={{header|C sharp}}==
<
using System.Collections.Generic;
using System.Linq;
Line 436:
static void Print(this IEnumerable<char> sequence) => Console.WriteLine(string.Join(" ", sequence));
}</
{{out}}
<pre>
Line 446:
=={{header|C++}}==
{{trans|D}}
<
#include <iostream>
#include <map>
Line 569:
return 0;
}</
{{out}}
<pre> 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2
Line 577:
=={{header|D}}==
<
import std.range;
import std.stdio;
Line 686:
group3();
group4();
}</
{{out}}
<pre>["1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2"]
Line 694:
=={{header|F_Sharp|F#}}==
<
// Wheels within wheels. Nigel Galloway: September 30th., 2019.
let N(n)=fun()->n
Line 714:
for n in 0..20 do printf "%d " (A4())
printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 739:
— a dictionary-like structure that is transformed into a lazy list which yields the expected sequence elements.
{{works with|Factor|0.99 2019-07-10}}
<
math.parser multiline peg.ebnf prettyprint prettyprint.custom
sequences strings ;
Line 774:
: .take ( n group -- )
list>> ltake list>array [ pprint bl ] each "..." print ;</
Now the interface defined above may be used:
<
rosetta-code.number-wheels ;
Line 802:
"Intersecting number wheel group:" print
[ . ] [ "Generates:" print 20 swap .take nl ] bi
] 4 napply</
{{out}}
<pre>
Line 831:
=={{header|Go}}==
<
import (
Line 919:
generate(wheels, "A", 20)
}
}</
{{out}}
Line 953:
terminating at the first digit found, and printing a map-accumulation of that recursion over a list of given length but arbitrary content.
<
import Data.List (mapAccumL)
import qualified Data.Map.Strict as M
Line 998:
wheelSets
putStrLn "\nInitial state of the wheel-sets:\n"
mapM_ print wheelSets</
{{Out}}
<pre>State of each wheel-set after 20 clicks:
Line 1,016:
=={{header|J}}==
Implementation:
<syntaxhighlight lang="j">
wheelgroup=:{{
yield_wheelgroup_=: {{
Line 1,033:
}}
grp=. cocreate ''
coinsert__grp 'wheelgroup
specs__grp=
wheel__grp=: ;{.wheels__grp=: {.every specs__grp
init__grp=: {{('inds';wheels)=:(0#~#specs);}.each specs}}
init__grp''
('gen_',(;grp),'_')~
}}
</syntaxhighlight>
Task examples:
<syntaxhighlight lang="j">
task=: {{y wheelgroup^:(1+i.20)_}}
task 'A 1 2 3'
Line 1,054:
task 'A 1 B C';'B 3 4';'C 5 B'
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4
</syntaxhighlight>
=={{header|Java}}==
<syntaxhighlight lang="java">
package intersectingNumberWheels;
import java.util.ArrayList;
Line 1,143:
}
}
</syntaxhighlight>
Output:
{A=[1, 2, 3]}
Line 1,159:
{{Trans|Haskell}}
{{Trans|Python}}
<
'use strict';
Line 1,319:
// MAIN ---
return main();
})();</
{{Out}}
<pre>Series and state of each wheel-set after 20 clicks:
Line 1,334:
{"A":"1DD"},{"D":"678"}
{"A":"1BC"},{"B":"34"},{"C":"5B"}</pre>
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
In this entry, a single wheel is simply represented by
a JSON object of the form { name: array }
where `name` is its name, and `array` is an array of the values on the wheel in the order
in which they would be read.
A set of of number of wheels can thus be represented simply as the sum of the objects corresponding to each wheel.
Thus the collection of illustrative number wheel groups can be defined as follows:
<syntaxhighlight lang="jq">
def wheels: [
{
"A": [1, 2, 3]
},
{
"A": [1, "B", 2],
"B": [3, 4]
},
{
"A": [1, "D", "D"],
"D": [6, 7, 8]
},
{
"A": [1, "B", "C"],
"B": [3, 4],
"C": [5, "B"]
}
];
</syntaxhighlight>
<syntaxhighlight lang="jq">
# read($wheel)
# where $wheel is the wheel to be read (a string)
# Input: a set of wheels
# Output: an object such that .value is the next value,
# and .state is the updated state of the set of wheels
def read($wheel):
# Input: an array
# Output: the rotated array
def rotate: .[1:] + [.[0]];
.[$wheel][0] as $value
| (.[$wheel] |= rotate) as $state
| if ($value | type) == "number"
then {$value, $state}
else $state | read($value)
end;
# Read wheel $wheel $n times
def multiread($wheel; $n):
if $n <= 0 then empty
else read($wheel)
| .value, (.state | multiread($wheel; $n - 1))
end;
def printWheels:
keys[] as $k
| "\($k): \(.[$k])";
# Spin each group $n times
def spin($n):
wheels[]
| "The number wheel group:",
printWheels,
"generates",
([ multiread("A"; $n) ] | join(" ") + " ..."),
"";
spin(20)
</syntaxhighlight>
'''Invocation'''
<pre>
jq -nr -f intersecting-number-wheels.jq
</pre>
{{output}}
<pre>
The 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 ...
The 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 ...
The 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 ...
The 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|Julia}}==
<
const d2 = Dict("A" => [["1", "B", "2"], 1], "B" => [["3", "4"], 1])
const d3 = Dict("A" => [["1", "D", "D"], 1], "D" => [["6", "7", "8"], 1])
Line 1,366 ⟶ 1,471:
foreach(testwheels, [d1, d2, d3, d4])
</
<pre>
Number Wheels:
Line 1,391 ⟶ 1,496:
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.util.stream.IntStream
Line 1,465 ⟶ 1,570:
endPosition = list.size - 1
}
}</
{{out}}
<pre>{A=[1, 2, 3]}
Line 1,478 ⟶ 1,583:
=={{header|Maple}}==
<syntaxhighlight lang="maple">
with(ArrayTools):
Line 1,531 ⟶ 1,636:
seq(currentValue(A), 1..20);
</syntaxhighlight>
{{out}}<pre>
Line 1,545 ⟶ 1,650:
=={{header|Nim}}==
<
type
Line 1,609 ⟶ 1,714:
{'A': "1 B 2", 'B': "3 4"}.generate(20)
{'A': "1 D D", 'D': "6 7 8"}.generate(20)
{'A': "1 B C", 'B': "3 4", 'C': "5 B"}.generate(20)</
{{out}}
Line 1,634 ⟶ 1,739:
=={{header|Perl}}==
{{trans|Julia}}
<
use warnings;
use feature 'say';
Line 1,658 ⟶ 1,763:
{'A' => [['1', 'D', 'D'], 0], 'D' => [['6', '7', '8'], 0]},
{'A' => [['1', 'B', 'C'], 0], 'B' => [['3', '4'], 0], 'C' => [['5', 'B'], 0]},
);</
{{out}}
<pre>A: 1, 2, 3
Line 1,677 ⟶ 1,782:
=={{header|Phix}}==
<!--<syntaxhighlight lang="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;">terms</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">wheels</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">pos</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">wheels</span><span style="color: #0000FF;">)),</span>
<span style="color: #000000;">wvs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">wheels</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">wheel</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">[</span><span style="color: #000000;">wheel</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">wheels</span><span style="color: #0000FF;">[</span><span style="color: #000000;">wheel</span><span style="color: #0000FF;">][</span><span style="color: #000000;">p</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">wheels</span><span style="color: #0000FF;">[</span><span style="color: #000000;">wheel</span><span style="color: #0000FF;">])?</span><span style="color: #000000;">2</span><span style="color: #0000FF;">:</span><span style="color: #000000;">p</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">pos</span><span style="color: #0000FF;">[</span><span style="color: #000000;">wheel</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">></span><span style="color: #008000;">'9'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">wheel</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">wvs</span><span style="color: #0000FF;">)</span>
<span
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span>
<span style="color: #000000;">rdx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">wheel</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">wheels</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"A123"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A1B2"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"B34"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A1DD"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"D678"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A1BC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"B34"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"C5B"</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">wheels</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">terms</span><span style="color: #0000FF;">(</span><span style="color: #000000;">wheels</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,716 ⟶ 1,824:
=={{header|Python}}==
===Python: Original class and generator based===
<
class INW():
Line 1,769 ⟶ 1,877:
]:
w = INW(**group)
print(f"{w}\n Generates:\n {first(w, 20)} ...\n")</
{{out}}
Line 1,797 ⟶ 1,905:
===Python: Simplified procedural===
<
while True:
nxt, w[name] = w[name][0], w[name][1:] + w[name][:1]
Line 1,817 ⟶ 1,925:
first = name[:-1] if first is None else first
gen = ' '.join(nextfrom(wheel, first) for i in range(20))
print(f" Generates:\n {gen} ...\n")</
{{out}}
Line 1,845 ⟶ 1,953:
Input is just a list of Python dicts, and depends on c-python dicts being odered by key insertion order.
<
nxt, w[name] = w[name][0], w[name][1:] + w[name][:1]
return nxt if '0' <= nxt[0] <= '9' else nextfromr(w, nxt)
Line 1,857 ⟶ 1,965:
first = next(group.__iter__())
gen = ' '.join(nextfromr(group, first) for i in range(20))
print(f" Generates:\n {gen} ...\n")</
{{out}}
Line 1,886 ⟶ 1,994:
{{Trans|Haskell}}
{{Works with|Python|3.7}}
<
from itertools import cycle, islice
Line 1,992 ⟶ 2,100:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>New state of wheel sets, after 20 clicks of each:
Line 2,013 ⟶ 2,121:
They could, for example, contain a nest that randomly selects a wheel to advance; <code>[ 1 [ 2 random table [ B C ] ] 2 ]</code> would do the same as <code>[ 1 B 2 ]</code>, except that on the second click of the wheel, instead of always advancing wheel <code>B</code>, <code>[ 2 random table [ B C ] ]</code> would be evaluated, causing either wheel <code>B</code> or wheel <code>C</code> to advance arbitrarily.
<
dup take behead
dup dip
Line 2,051 ⟶ 2,159:
( So we reset it to [ 3 4 ]. )
20 times [ A echo sp ] cr</
{{out}}
Line 2,063 ⟶ 2,171:
(formerly Perl 6)
A succinct Raku example using a few additional language features. Wheels are implemented as infinite repeating sequences, allowing a single iterator to keep track of the current position. This means the code contains no position tracking whatsoever.
<syntaxhighlight lang="raku" line>
#| advance rotates a named wheel $n by consuming an item from an infinite sequence. It is called
#| from within a gather block and so can use take in order to construct an infinite, lazy sequence
Line 2,092 ⟶ 2,200:
#| state variables are only initialised once, and are kept between invocations.
==> map({ state $i = 1; say "Group {$i++}, First 20 values: $_[^20]" })
</
<pre>
Group 1, First 20 values: 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2
Line 2,105 ⟶ 2,213:
This REXX program uses ''numbers'' (any form), not ''digits'' (for the values on the wheels).
<
@.= /*initialize array to hold the wheels. */
parse arg lim @.1 /*obtain optional arguments from the CL*/
Line 2,154 ⟶ 2,262:
end /*forever*/ /* [↑] found a number, now use FIRST.*/
end /*dummy*/ /*"DUMMY" is needed for the ITERATE. */
say '('lim "results): " strip($); say; say; return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,194 ⟶ 2,302:
=={{header|Ruby}}==
<
{A: [1, :B, 2], B: [3, 4]},
{A: [1, :D, :D], D: [6, 7, 8]},
Line 2,209 ⟶ 2,317:
puts res.join(" "),""
end
</syntaxhighlight>
{{out}}
<pre>{:A=>[1, 2, 3]}
Line 2,225 ⟶ 2,333:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 2,265 ⟶ 2,373:
End Sub
End Module</
{{out}}
<pre>1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2
Line 2,277 ⟶ 2,385:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<
import "./sort" for Sort
import "./fmt" for Fmt
var Wheel = Struct.create("Wheel", ["next", "values"])
Line 2,352 ⟶ 2,460:
printWheels.call(wheels)
generate.call(wheels, "A", 20)
}</
{{out}}
Line 2,382 ⟶ 2,490:
=={{header|zkl}}==
<
ws:=wheels.pump(Dictionary(),fcn([(k,v)]){ return(k,Walker.cycle(v)) }); // new Dictionary
Walker.zero().tweak(fcn(w,wheels){
Line 2,390 ⟶ 2,498:
}
}.fp("A",ws)) // assume wheel A exists and is always first
}</
<
Dictionary("A",T(1,"B",2), "B",T(3,4)),
Dictionary("A",T(1,"D","D"), "D",T(6,7,8)),
Line 2,399 ⟶ 2,507:
ws.pump(String,fcn([(k,v)]){ " %s: %s\n".fmt(k,v.concat(" ")) }).print();
println("-->",intersectingNumberWheelsW(ws).walk(20).concat(" "));
}</
{{out}}
<pre>
|