Intersecting number wheels: Difference between revisions
Content added Content deleted
(Add Perl 6 example) |
(→{{header|REXX}}: added the REXX computer programming language for this task.) |
||
Line 585: | Line 585: | ||
Generates: |
Generates: |
||
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|REXX}}== |
|||
Quite a bit of the code deals with detecting (and issuing) errors in the specification of the wheel sets. |
|||
<lang rexx>/*REXX program expresses numbers from intersecting number wheels (or wheel sets). */ |
|||
@.= /*initialize array to hold the wheels. */ |
|||
parse arg lim @.1 /*obtain optional arguments from the CL*/ |
|||
if lim='' | lim="," then lim= 20 /*Not specified? Then use the default.*/ |
|||
if @.1='' | @.1="," then do; @.1= ' A: 1 2 3 ' |
|||
@.2= ' A: 1 B 2, B: 3 4 ' |
|||
@.3= ' A: 1 D D, D: 6 7 8 ' |
|||
@.4= ' A: 1 B C, B: 3 4, C: 5 B ' |
|||
end |
|||
do j=1 while @.j\=''; call build /*construct the wheel set (gear sets).*/ |
|||
call run /*execute " " " " " */ |
|||
end /*j*/ |
|||
exit /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
error: say; say; say '***error***' arg(1); say; say; exit 12 |
|||
isLet: return datatype( arg(1), 'M') & length( arg(1) )==1 |
|||
isNum: return datatype( arg(1), 'N') |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
build: @wn= 'wheel name'; first=; @wnnfbac= 'wheel name not followed by a colon:' |
|||
@gn= 'gear name' ; gear.=; say copies('═', 79) |
|||
say 'building wheel group for: ' @.j; wheels= space(@.j); upper wheels |
|||
do i=1 while wheels\=''; parse var wheels w gears ',' wheels; L= length(w) |
|||
if L==2 then do; !.i= left(w, 1) /*obtain the 1-char gear name.*/ |
|||
if right(w, 1)\==':' then call error @wnnfbac w |
|||
if \isLet(!.i) then call error @wn "not a letter:" w |
|||
end |
|||
else call error "first token isn't a" @wn':' w |
|||
if i==1 then first= !.1 /*Is this is the 1st wheel set? Use it*/ |
|||
if first=='' then call error "no wheel name was specified." |
|||
n= !.i /*obtain the name of the 1st wheel set.*/ |
|||
gear.n.0= 1 /*initialize default 1st gear position.*/ |
|||
say ' setting gear.name:' n ' gears=' gears |
|||
do g=1 for words(gears); _= word(gears, g) |
|||
if isNum(_) | isLet(_) then do; gear.n.g= _; iterate; end |
|||
call error @gn "isn't a number or a gear name:" _ |
|||
end /*g*/ |
|||
end /*i*/; return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
run: say; say center(' running the wheel named ' first" ", 79, "─"); $= |
|||
do #=0 by 0 until words($)==lim; n= first |
|||
z= gear.n.0; x= gear.n.z |
|||
z= z + 1; gear.n.0= z; if gear.n.z=='' then gear.n.0= 1 |
|||
if isNum(x) then do; $= $ x; iterate; end /*found a number, use it.*/ |
|||
xx= x /*different gear, keep switching until #.*/ |
|||
do forever; nn= xx |
|||
if gear.nn.0=='' then call error "a gear is using an unknown gear name:" x |
|||
zz= gear.nn.0; xx= gear.nn.zz |
|||
zz= zz + 1; gear.nn.0= zz; if gear.nn.zz=='' then gear.nn.0= 1 |
|||
if isNum(xx) then do; $= $ xx; iterate #; end |
|||
end /* [↑] found a number, now use FIRST. */ |
|||
end /*until*/ |
|||
say '('lim "results): " strip($); say; say; return</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
═══════════════════════════════════════════════════════════════════════════════ |
|||
building wheel group for: A: 1 2 3 |
|||
setting gear.name: A gears= 1 2 3 |
|||
───────────────────────── running the wheel named A ────────────────────────── |
|||
(20 results): 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 |
|||
═══════════════════════════════════════════════════════════════════════════════ |
|||
building wheel group for: A: 1 B 2, B: 3 4 |
|||
setting gear.name: A gears= 1 B 2 |
|||
setting gear.name: B gears= 3 4 |
|||
───────────────────────── running the wheel named A ────────────────────────── |
|||
(20 results): 1 3 2 1 4 2 1 3 2 1 4 2 1 3 2 1 4 2 1 3 |
|||
═══════════════════════════════════════════════════════════════════════════════ |
|||
building wheel group for: A: 1 D D, D: 6 7 8 |
|||
setting gear.name: A gears= 1 D D |
|||
setting gear.name: D gears= 6 7 8 |
|||
───────────────────────── running the wheel named A ────────────────────────── |
|||
(20 results): 1 6 7 1 8 6 1 7 8 1 6 7 1 8 6 1 7 8 1 6 |
|||
═══════════════════════════════════════════════════════════════════════════════ |
|||
building wheel group for: A: 1 B C, B: 3 4, C: 5 B |
|||
setting gear.name: A gears= 1 B C |
|||
setting gear.name: B gears= 3 4 |
|||
setting gear.name: C gears= 5 B |
|||
───────────────────────── running the wheel named A ────────────────────────── |
|||
(20 results): 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}}== |