Intersecting number wheels

From Rosetta Code
Revision as of 09:36, 28 September 2019 by rosettacode>Paddy3118 (New draft task with python example.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Intersecting number wheels is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A number wheel has:

  • A name which is an uppercase letter.
  • A set of ordered values which are either numbers or names.


A number is generated/yielded from a named wheel by:

1. Starting at the first value of the named wheel and advancing through subsequent values and wrapping around to the first value to form a "wheel":
1.a If the value is a number, yield it.
1.b If the value is a name, yield the next value from the named wheel
1.c Advance the position of this wheel.

Given the wheel

A: 1 2 3

the number 1 is first generated, then 2, then 3, 1, 2, 3, 1, ...

Note: When more than one wheel is defined as a set of intersecting wheels then the first named wheel is assumed to be the one that values are generated from.

Examples

Given the wheels:

   A: 1 B 2
   B: 3 4

The series of numbers generated starts:

   1, 3, 2, 1, 4, 2, 1, 3, 2, 1, 4, 2, 1, 3, 2...

The intersections of number wheels can be more complex, (and might loop forever), and wheels may be multiply connected.

Note: If a named wheel is referenced more than once by one or many other wheels, then there is only one position of the wheel that is advanced by each and all references to it.

E.g.

 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 ...    
Task

Generate and show the first twenty terms of the sequence of numbers generated from these groups:

   Intersecting Number Wheel group:
     A:  1 2 3
   
   Intersecting Number Wheel group:
     A:  1 B 2
     B:  3 4
   
   Intersecting Number Wheel group:
     A:  1 D D
     D:  6 7 8
   
   Intersecting Number Wheel group:
     A:  1 B C
     B:  3 4
     C:  5 B

Show your output here, on this page.

Python

<lang python>from itertools import islice

class INW():

   """
   Intersecting Number Wheels
   represented as a dict mapping
   name to tuple of values.
   """
   def __init__(self, **wheels):
       self._wheels = wheels
       self.isect = {name: self._wstate(name, wheel) 
                     for name, wheel in wheels.items()}
   
   def _wstate(self, name, wheel):
       "Wheel state holder"
       assert all(val in self._wheels for val in wheel if type(val) == str), \
              f"ERROR: Interconnected wheel not found in {name}: {wheel}"
       pos = 0
       ln = len(wheel)
       while True:
           nxt, pos = wheel[pos % ln], pos + 1
           yield next(self.isect[nxt]) if type(nxt) == str else nxt
               
   def __iter__(self):
       base_wheel_name = next(self.isect.__iter__())
       yield from self.isect[base_wheel_name]
       
   def __repr__(self):
       return f"{self.__class__.__name__}({self._wheels})"
   
   def __str__(self):
       txt = "Intersecting Number Wheel group:"
       for name, wheel in self._wheels.items():
           txt += f"\n  {name+':':4}" + ' '.join(str(v) for v in wheel)
       return txt

def first(iter, n):

   "Pretty print first few terms"
   return ' '.join(f"{nxt}" for nxt in islice(iter, n))

if __name__ == '__main__':

   for group in[
     {'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')}, # 135143145...
    ]:
       w = INW(**group)
       print(f"{w}\n  Generates:\n    {first(w, 20)} ...\n")</lang>
Output:
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 ...