伪代码执行解释

问题描述 投票:0回答:1

(https://i.stack.imgur.com/SMmq1.png)

请检查上面的图片以查看伪代码

function doSome(array) {
  let value = array[0];
  let index = 0;
  let output = [];

  for (let x = 1; x < array.length; x++) {
    if (array[x] * array[x - 1] > 0) {
      if (value < 0 && array[x] < value) {
        value = array[x];
        index = x;
      }
      if (value >= 0 && array[x] > value) {
        value = array[x];
        index = x;
      }
    } else {
      output.push([index, value]);
      value = array[x];
      index = x;
    }
  }

  return output;
}

// Test with the given array
const inputArray = [1, 4, 2, -2, -9, 10, 2, 12, 2, -4, -4, -4, -4, 2, 6, 7];
const values = doSome(inputArray);
console.log(values); // logs [ [ 1, 4 ], [ 4, -9 ], [ 7, 12 ], [ 9, -4 ] ]

这是我为上述伪代码编写的javascript代码,有人可以解释一下这段代码,我无法遵循这里的模式。我知道这段代码不适合查找峰值元素(大于相邻元素的元素),但是它遵循哪种模式在做什么?

javascript python debugging pseudocode
1个回答
0
投票

它查看具有相同符号(正数或负数)的数字组,输出返回这些组的最大值是什么以及最大值在哪里。

© www.soinside.com 2019 - 2024. All rights reserved.