Zero Array Questions — 弄清楚解决方案的工作原理

huangapple go评论74阅读模式
英文:

Zero Array Questions -- Figuring Out How a Solution Works

问题

代码的描述:
这段代码的目标是判断给定的数组是否是"zero-plentiful",也就是说数组中包含多个零,并且每个零序列至少有4个连续的零。

这段代码的解释:
代码使用了一个zeroPlentiful函数来实现这一目标。以下是代码的逐步解释:

  1. 创建一个空数组counter,用于存储每个零序列的长度。
  2. 创建一个变量index,初始化为0,用于追踪counter数组的索引。

接下来是forEach循环,它遍历输入数组arr中的每个元素,并执行一个回调函数。这个回调函数的目的是确定每个零序列的长度,以便后续的判断。

  1. 对于每个数组元素,回调函数会检查元素的值。如果元素的值为0,它会执行以下操作:
    • 如果counter数组中已经有一个与index相对应的元素,就将该元素的值加1,表示已经找到一个连续的零。
    • 如果counter数组中没有一个与index相对应的元素,就创建一个,并将其值初始化为1。
  2. 如果元素的值不是0(即非零),则将index设置为counter数组的长度,表示当前零序列结束,需要开始统计下一个零序列。

最后,代码返回的结果是根据counter数组中的值来确定是否输入数组是"zero-plentiful"。如果counter数组中的每个元素都大于或等于4(即每个零序列至少包含4个连续的零),则返回counter数组的长度(表示零序列的个数)。否则,返回0,表示输入数组不是"zero-plentiful"。

这段代码的目标是通过迭代输入数组,查找零序列,并检查它们的长度是否符合条件。如果所有零序列都满足条件,就返回它们的数量,否则返回0。

英文:

https://www.codewars.com/kata/59e270da7997cba3d3000041/javascript

DESCRIPTION:
An array is called zero-plentiful if it contains multiple zeros, and every sequence of zeros is at least 4 items long.

Your task is to return the number of zero sequences if the given array is zero-plentiful, oherwise 0.

Examples
[0, 0, 0, 0, 0, 1] --> 1

1 group of 5 zeros (>= 4), thus the result is 1

[0, 0, 0, 0, 1, 0, 0, 0, 0] --> 2

2 group of 4 zeros (>= 4), thus the result is 2

[0, 0, 0, 0, 1, 0] --> 0
1 group of 4 zeros and 1 group of 1 zero (< 4)
every sequence of zeros must be at least 4 long, thus the result is 0

[0, 0, 0, 1, 0, 0] --> 0
1 group of 3 zeros (< 4) and 1 group of 2 zeros (< 4)

[1, 2, 3, 4, 5] --> 0
no zeros

[] --> 0
no zeros

function zeroPlentiful(arr) {
  let counter = [];
  let index = 0;
  arr.forEach((num, idx) =&gt; {
   if (num === 0) {
     counter[index] = counter[index] ? counter[index] + 1 : 1;
   } else {
     index = counter.length;
   } 
  });
  return counter.every(item =&gt; item &gt;= 4) ? counter.length : 0;
}

Would someone please give me a play-by-play about what's going on in this code? I've attempted this question a bunch of times and looked at all the solutions, but I'm still having trouble figuring out what's going on. I understand that the forEach is looking for 0s and non-0 numbers but, beyond that, I'm not sure what's happening.

答案1

得分: 0

这段代码实际上非常简单,两个主要变量是counterindexcounter用来跟踪数组中零的数量。然后,它遍历数组,并验证当前位置是否为零,如果是,则将index位置的counter递增。

因此,对于第一个数组[0,0,0,0,0,1]counter[index]将为5,因为有5个连续的零,而index的值没有从其原始值增加。

如果值不是0,则不会在counter上递增任何内容,但会递增index,因为零的序列被打破了,现在计数器从新的位置开始。

基本上,它创建了一个包含每个序列中零的数量的数组,然后return语句简单地运行了每个函数,并验证序列是否为4个或更多。

英文:

The code is quite simple actually, the two main variables are counter and index, counter keeps track of the number of zeros in the array. Then, it iterates through the array, and validates if the current position is a zero, and if it is, the counter in the position of index is incremented.

So for the first array, [0,0,0,0,0,1], the counter[index] will be 5, because there are 5 zeros in sequence, and index was not incremented from it's original value.

And if the value is not 0, then it doesn't increment anything on the counter but increments index, because the sequence of 0s was broken, and now the counter starts on a new position.

Basically it creates an array with the number of zeros in each sequence, then the return statement simply runs the every function, and validates if the sequence was 4 or more.

huangapple
  • 本文由 发表于 2023年3月4日 04:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定