A previous advent answer got me thinking about state machines, so I wrote one for this problem. The extra initial state ruins the natural mapping
between states and numbers, but I think it's unavoidable?  Also someone
could probably write a clever verb to generate the table.


NB. state machine -- can be confusing because rows/states 1,2,3,.. represent numbers 0,1,2,..
S=: 0 10#: 10* ". }. [;._2 noun define
0    1    2    3    4    5    6    7    8    9
1.1  2.1  3.1  4.1  5.1  6.1  7.1  8.1  9.1  10.1   NB. initial
1.0  2.2  3.2  4.2  5.2  6.2  7.2  8.2  9.2  10.2   NB. 0
1.2  2.0  3.2  4.2  5.2  6.2  7.2  8.2  9.2  10.2   NB. 1
1.2  2.2  3.0  4.2  5.2  6.2  7.2  8.2  9.2  10.2   NB. 2
1.2  2.2  3.2  4.0  5.2  6.2  7.2  8.2  9.2  10.2   NB. 3
1.2  2.2  3.2  4.2  5.0  6.2  7.2  8.2  9.2  10.2   NB. 4
1.2  2.2  3.2  4.2  5.2  6.0  7.2  8.2  9.2  10.2   NB. 5
1.2  2.2  3.2  4.2  5.2  6.2  7.0  8.2  9.2  10.2   NB. 6
1.2  2.2  3.2  4.2  5.2  6.2  7.2  8.0  9.2  10.2   NB. 7
1.2  2.2  3.2  4.2  5.2  6.2  7.2  8.2  9.0  10.2   NB. 8
1.2  2.2  3.2  4.2  5.2  6.2  7.2  8.2  9.2  10.0   NB. 9
)
v=:[: ,@:((#,{.)every) (0;S)&;:
smoutput $ v^:40 Input=: 1 1 1 3 2 2 2 1 1 3
smoutput $ v^:50 Input
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to