How to resolve the algorithm Intersecting number wheels step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Intersecting number wheels step by step in the Wren programming language

Table of Contents

Problem Statement

A number wheel has: A number is generated/yielded from a named wheel by: Given the wheel 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. Given the wheels: The series of numbers generated starts: 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. Generate and show the first twenty terms of the sequence of numbers generated from these groups: Show your output here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Intersecting number wheels step by step in the Wren programming language

Source code in the wren programming language

import "./dynamic" for Struct
import "./sort" for Sort
import "./fmt" for Fmt

var Wheel = Struct.create("Wheel", ["next", "values"])

var generate = Fn.new { |wheels, start, maxCount|
    var count = 0
    var w = wheels[start]
    while (true) {
        var s = w.values[w.next]
        var v = Num.fromString(s)
        w.next = (w.next + 1) % w.values.count
        wheels[start] = w
        if (v) {
            System.write("%(v) ")
            count = count + 1
            if (count == maxCount) {
                System.print("...\n")
                return
            }
        } else {
            while (true) {
                var w2 = wheels[s]
                var ss = s
                s = w2.values[w2.next]
                w2.next = (w2.next + 1) % w2.values.count
                wheels[ss] = w2
                v = Num.fromString(s)
                if (v) {
                    System.write("%(v) ")
                    count = count + 1
                    if (count == maxCount) {
                        System.print("...\n")
                        return
                    }
                    break
                }
            }
        }
    }
}

var printWheels = Fn.new { |wheels|
    var names = []
    for (name in wheels.keys) names.add(name)
    Sort.quick(names)
    System.print("Intersecting Number Wheel group:")
    for (name in names) {
        Fmt.print("  $s: $n", name, wheels[name].values)
    }
    System.write("  Generates:\n    ")
}

var wheelMaps = [
    {
        "A": Wheel.new(0, ["1", "2", "3"])
    },
    {
        "A": Wheel.new(0, ["1", "B", "2"]),
        "B": Wheel.new(0, ["3", "4"])
    },
    {
        "A": Wheel.new(0, ["1", "D", "D"]),
        "D": Wheel.new(0, ["6", "7", "8"])
    },
    {
        "A": Wheel.new(0, ["1", "B", "C"]),
        "B": Wheel.new(0, ["3", "4"]),
        "C": Wheel.new(0, ["5", "B"])
    }
]
for (wheels in wheelMaps) {
    printWheels.call(wheels)
    generate.call(wheels, "A", 20)
}


  

You may also check:How to resolve the algorithm Globally replace text in several files step by step in the Jsish programming language
You may also check:How to resolve the algorithm Get system command output step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Additive primes step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Generator/Exponential step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the PureBasic programming language