Home  >  Q&A  >  body text

javascript - codewars一道推理题,不理解,题名字是 Unflatten a list

Continue with the next number/element AFTER this taken elements/numbers.

The direction is always the same as the actual run.

Array: [4, 5, 1, 7, 1] Depth: 2 -> [[ 4, [ 5, 1, 7 ] ], 1]

Steps:
First run: (start from left side!)

  1. The first number is 4. The number is smaller than the number of remaining elements, so it is the remainder-value (4 / 5 -> remainder 4).
    So 4 numbers (4, 5, 1, 7) are added as sub-array in the new array.

  2. The next number is 1. It is smaller than 3, so the 1 is added direct to the new array.
    Now we have --> [[4, 5, 1, 7], 1]

Second run: (start from right side!)

  1. The last number (first from other side) is 1. So the 1 is added direct to the new array.

  2. The next element is the sub-array. So we use the rules for this.
    2a.The last number is 7. There are 4 elements in the array. So for the next decision you have to

take the remainder from 7 / 4 -> 3. So 3 numbers (5, 1, 7) are added as sub-array in the
new array.
2b.Now there is the 4 and only one element last in this array. 4 / 1 -> remainder 0. It is smaller
than 3. So the 4 is added direct to the new array.
Now we have --> [[ 4, [ 5, 1, 7 ] ], 1]

伊谢尔伦伊谢尔伦2653 days ago216

reply all(0)I'll reply

No reply
  • Cancelreply