Intersecting Number Wheels
October 1, 2019
Today’s exercise is a new task from Rosetta Code:
A number wheel has a name which is an uppercase letter and a set of ordered values which are either numbers or names. A number is generated from a named wheel by starting at the first value of the names wheel and advancing through subesquent values then wrapping around to the first value to form a “wheel”, according to the following rules: If the value is a number, yield it; if the value is a name, yield the next value from the named wheel; advance the position of the wheel. For instance, given two wheels A: 1 B 2 and B: 3 4, the sequence generated for wheel A is 1 3 2 1 4 2 1 3 2 1 4 2 ….
Your task is to write a program to calculate the sequences generated by intersecting number wheels. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Here is a very simple solution using only R7RS Scheme plus
circular-list from SRFI 1 (and unfold for a demo). It is avoids eval
but uses destructive updates. It also requires the wheels to be
defined without forward references, which could be considered a
feature that disallows wheels like A = (B) with B = (A).
Output:
Here’s a C++ solution, read the wheels from the command line, 1st arg is for A, 2nd for B etc. Store in array and use second array to store current offset for each wheel:
A better version:
Here’s a solution in Python.
Output: