计算平均值和模式

发布于 2025-01-22 14:41:55 字数 651 浏览 0 评论 0原文

我应该返回给定数组的平均值和模式。但是,对于模式,如果有两种模式,那么我需要在数组中返回第一个模式。例如,如果阵列为statsfinder([500、400、400、375、375、300、350、325、300]),输出应为:[368.75,400],因为400在300前的数组中发生了400。接近解决方案,我无法获得第一个发生模式。任何人都知道如何修复我的代码以获得第一个发生模式? 这是我的解决方案:

function statsFinder(array) { 

let length=array.length
let sum=array.reduce((acc,curr)=>{
    return acc+curr
})
let mean=sum/length
let obj=array.reduce((acc,curr)=>{
 acc[curr]=acc[curr] ? acc[curr]+1:1
 if(acc[curr]>acc.max){ 
   acc.max=acc[curr]
 }
  return acc

},{max:1}) 

let mode=0
for(let key in obj){ 
  if (mode[key]===mode.max){ 
    mode=parseInt(key)
    }
    return [mean, mode]
  }
  
}

I am supposed to return the mean and the mode of a given array. For the mode though, if there are two modes then I need to return the first one in the array. For example if the array was statsFinder([500, 400, 400, 375, 300, 350, 325, 300]) the output should be: [368.75, 400] since 400 occurs in the array before 300. Although I believe I am close to the solution I can't get the first occurring mode. Anyone have any idea how I could fix my code to get the first occurring mode??
Here's my solution for it:

function statsFinder(array) { 

let length=array.length
let sum=array.reduce((acc,curr)=>{
    return acc+curr
})
let mean=sum/length
let obj=array.reduce((acc,curr)=>{
 acc[curr]=acc[curr] ? acc[curr]+1:1
 if(acc[curr]>acc.max){ 
   acc.max=acc[curr]
 }
  return acc

},{max:1}) 

let mode=0
for(let key in obj){ 
  if (mode[key]===mode.max){ 
    mode=parseInt(key)
    }
    return [mean, mode]
  }
  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他不在意 2025-01-29 14:41:55

您要做的:

  1. 扫描整个数组计算每个唯一值的频率。一路上记住最大计数。
  2. 之后,扫描值和频率图的图,寻找的值与最大计数相同。

尽管您应该在第二次扫描中修复凹痕,以便您可以看到为什么它不起作用,但可能会奏效。您想要这个:

  for(let key in obj){ 
    if (mode[key]===mode.max){ 
      mode=parseInt(key)
      return [mean, mode]
    }
  }

或者,更好的是:

  for(let key in obj){ 
    if (mode[key]===mode.max){ 
      mode=parseInt(key)
      break
    }
  }
  return [mean, mode]

请注意,依靠JavaScript对象中属性的迭代顺序不是最好的做法,尽管它可能“大多数情况下”。如果您需要按插入顺序进行迭代,则应使用MAP(如果可能)。

但是整个扫描是愚蠢的。您可以在计数频率时记住与最大计数相关的值:

  acc[curr] = acc[curr] ? acc[curr]+1 : 1
  if (acc[curr] > acc.max) { 
    acc.maxval = curr
    acc.max = acc[curr]
  }

然后,您知道该模式而无需再次搜索它,而且您无需将值转换回一个数字。

What you do:

  1. Scan the entire array counting the frequency of each unique value. Along the way, remember the maximum count.
  2. Afterwards, scan the map of values and frequencies looking for the value which has the same count as the maximum count.

That could work, sort of, although you should fix the indentation in the second scan so that you can see why it doesn't work. You wanted this:

  for(let key in obj){ 
    if (mode[key]===mode.max){ 
      mode=parseInt(key)
      return [mean, mode]
    }
  }

or, imho better:

  for(let key in obj){ 
    if (mode[key]===mode.max){ 
      mode=parseInt(key)
      break
    }
  }
  return [mean, mode]

Note that it's not best practice to rely on the order of iteration of properties in a Javascript object, although it will probably work "most of the time". If you require iteration in insertion order, you should use a Map, if possible.

But that whole scan is silly. You could just remember the value associated with the maximum count while you count frequencies:

  acc[curr] = acc[curr] ? acc[curr]+1 : 1
  if (acc[curr] > acc.max) { 
    acc.maxval = curr
    acc.max = acc[curr]
  }

Then you know the mode without needing to search for it again, and moreover you don't have any need to convert the value back to an number.

枫以 2025-01-29 14:41:55

只是面临同样的挑战,到目前为止,这是我的代码,希望它对您有所帮助。它只是通过3/5测试,因此仍然可以改进。

function statsFinder(array) {
  // Write your code here
    let sum = array.reduce((partialSum, a) => partialSum + a, 0);
    const mean = sum / array.length;
    let answer = [mean];
  for(let i=0; i<array.length;i++) {
      let current = array[0];
      if(current === array[1]) {
        answer.push(current);
       break;
      } else {
        array.shift();
      }
  }
  return answer;
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

Just got to the same challenge and so far this is my code, hope it helps you. It's only passing 3/5 tests, so still room for improving.

function statsFinder(array) {
  // Write your code here
    let sum = array.reduce((partialSum, a) => partialSum + a, 0);
    const mean = sum / array.length;
    let answer = [mean];
  for(let i=0; i<array.length;i++) {
      let current = array[0];
      if(current === array[1]) {
        answer.push(current);
       break;
      } else {
        array.shift();
      }
  }
  return answer;
}

console.log(statsFinder([500, 400, 400, 375, 300, 350, 325, 300]))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文