Home > Enterprise >  loop threw an array of arrays in recursion (algorithm problem)
loop threw an array of arrays in recursion (algorithm problem)

Time:10-31

I kinda want to create a feature which is most often known in factories. Kind of rotation system, where people swap their station, while others are on break.

via DragnDrop I create/add/update a Map and convert this map to an array of arrays like this:

let example1 = [
    ['John',    ['99', '1']],
    ['Jo',      ["1", "3"]],
    ["Alpha",   ["99", "4"]],
    ["Beta",    ["3", "2"]],
    ["Gamma",   ["2", "99"]],
    ["Delta",   ["4", "5"]],
    ["Maria",   ["5", "6"]],
    ["Epsilon", ["6", "99"]],
];

the first number is for the old Position, the second number is for the new position. The number 99 stands for beeing on break. the output for above example should be something like:

outputExample1 = [
    ["John", "1", "3", "2"], 
    ["Alpha", "4, "5", "6"] 
]

so every sequenze and recursion should start with a breaker (=number 99) and end with a breaker. in above example: after placing "john" at station one, it should "search" where the guy/ladie from station 1 is "now". in this example, he is on 3. now search again where the one from statin 3 is now. (=2) ... until the number is 99, which is the case at this example. thats why I started to filter the original array in 'activ'(<99) and 'breakers'(==99). I tried so many ways and fail continously ( while loops ends in an endless loop, outputs which are totally wrong), because i dont find a nice recursion.

Any hints are very preciated

PS: please consider that above array is "finished" to provide a good example and may not be completed on the fly (via drag and drop). Meaning: if I start to drag the sequenze is for sure not completed.

Edit: there will be for sure at least ONE '99'. if not no sequenze and no output. Also there are no duplicates on the 'new position' except 99er. (which are the starters anyway

CodePudding user response:

You could take a sequential approach and visit all nodes in the order, you have. Then take a nested property for the most top items and keep the relation with the nodes. At the end return just top nodes.

If necessary take the entries of the result for an array of key/value pairs.

const
    data = [['John',    ['99', '1']], ['Jo',      ["1", "3"]], ["Alpha",   ["99", "4"]], ["Beta",    ["3", "2"]], ["Gamma",   ["2", "99"]], ["Delta",   ["4", "5"]], ["Maria",   ["5", "6"]], ["Epsilon", ["6", "99"]]],
    relations = data
        .reduce((r, [top, [from, to]]) => {
            if (to === '99') return r;
            if (from === '99') {
                r[to] = r.top[top] = [to];
            } else {
                r[from].push(to);
                r[to] = r[from];
            }
            return r;
        }, { top: {} })
        .top;

console.log(relations);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Don't use recursion. You can do same thing without recursion with loops. Using recursion is a bad practice.

  • Related