简体   繁体   English

如何使用递归计算数组中数字的实例?

[英]How to count instances of numbers in an array using recursion?

I try to write a function (called: tally) using recursion (part of the exercise) to scan through an array of numbers and return an object with the numbers as key and the number of instances as value.我尝试使用递归(练习的一部分)编写一个 function(称为:tally)来扫描一个数字数组并返回一个 object,其中数字作为键,实例数作为值。

Example:例子:

tally([2,3,4,5,5,5,5,5,5,5,6,7,,6,7,6,7,5,4,3,4,5,5,6])
//{2: 1, 3: 2, 4: 3, 5: 10, 6: 4, 7: 3}

I created the framework but i am not sure about the syntax to make it work:我创建了框架,但我不确定使其工作的语法:

function tally(arr) {
    var obj = {}
    if (/*check if object ('obj') has a key corresponding to the array element*/) {
        //increase key's value by onee
    } else {
        //add key with value of 1
    }
    return obj
};

Any hints to complete the recursion function above?完成上述递归 function 的任何提示? Please try to stick to my structure in your answers as much as possible since this is part of an exercise.请尽量在你的答案中坚持我的结构,因为这是练习的一部分。

Here you are:给你:

function tally(arr) {
    if (arr.length == 0) {
        return {}
    }
    var value = arr.pop()
    var obj = tally(arr)
    if (value in obj) {
        obj[value] += 1
    } else {
        obj[value] = 1
    }
    return obj
};

EDIT: It can also be done using slice() instead of pop() :编辑:也可以使用slice()而不是pop()来完成:

function tally(arr) {
    if (arr.length == 0) {
        return {}
    }
    var value = arr[0]
    var obj = tally(arr.slice(1))
    if (value in obj) {
        obj[value] += 1
    } else {
        obj[value] = 1
    }
    return obj
};

ok, so you are asked to do a recursion just for the sake of it.好的,所以你被要求做一个递归只是为了它。

This could be done (albeit is hacky) passing an extra parameter to tally .这可以通过将一个额外的参数传递给tally来完成(尽管这很老套)。 When you declare a function in vanilla js you can actually feed it extra stuff.当你在 vanilla js 中声明一个 function 时,你实际上可以给它额外的东西。 So, in each recursion, pass obj as a second parameter:因此,在每次递归中,将obj作为第二个参数传递:

EDIT Thanks @Bergi, you're right.编辑谢谢@Bergi,你是对的。 I'll edit the code我将编辑代码

 function tally(arr) { let obj = arguments.length>1? arguments[1]: {}; if(arr.length===0) { return obj; } let next_number=arr.pop(); obj[next_number]=obj[next_number]||0; obj[next_number]++; return tally(arr,obj); }; let inputArr = [2,3,4,5,5,5,5,5,5,5,6,7,6,7,6,7,5,4,3,4,5,5,6], outputObj=tally(inputArr); console.log(outputObj); console.log({outputEmpty:tally([])});

I am not sure how to guide you to an answer without giving it away entirely, but this is what I would recommend.我不确定如何在不完全放弃的情况下引导您找到答案,但这是我的建议。 (There are some problems such as you destroy arr in the process that you may want to consider) (过程中有你销毁arr等问题你可能要考虑)

function tally(arr, obj) {

    // if the length is zero we've gone through every value
    if(arr.length === 0)
        return obj

    // create obj if we didn't provide it
    if(obj === undefined)
        obj = {}

    // pull the last value from arr
    let val = arr.pop()

    if (/*check if object ('obj') has a key corresponding to the array element*/) {
        //increase key's value by onee
    } else {
        //add key with value of 1
    }

    // move onto the next value
    return tally(arr,obj)

}

EDIT: took @Bergi's input编辑:接受@Bergi 的意见

Using extra parameter for an index, i , the result, r -使用索引的额外参数i ,结果r -

 const plus1 = (k = "", r = {}) => ( k in r? r[k] += 1: r[k] = 1, r ) const tally = (a = [], i = 0, r = {}) => i >= a.length? r: tally ( a, i + 1, plus1(a[i], r) ) console.log(tally([2,3,4,5,5,5,5,5,5,5,6,7,,6,7,6,7,5,4,3,4,5,5,6]))

Output Output

{
  "2": 1,
  "3": 2,
  "4": 3,
  "5": 10,
  "6": 4,
  "7": 3,
  "undefined": 1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM