英文:
Is there a function to calculate all possible sum of an array of number between them?
问题
I am currently facing a problem for a few days already.
I have a value, say N = 0.8
And an array of numbers, say tab = [0.4, 0.1, 0.4, 0.5]
Here we can see that the values at index 0 and 2 are 0.8, but I can't program this in a clean way, I end up with too many for loops (3 to be precise).
I tried to make a naive algorithm.
Create a variable value = 0
Two for loops, add the first element in the first loop and the others in the second one while checking that value + tab[j] <= 0.8
, in some cases it works and in others, it's less than 0.8.
英文:
I am currently facing a problem for a few days already.
I have a value, say N = 0.8
And an array of numbers, say tab = [0.4, 0.1, 0.4, 0.5]
Here we can see that the values at index 0 and 2 are 0.8, but I can't program this in a clean way, I end up with too many for loops (3 to be precise).
I tried to make a naive algorithm.
Create a variable value = 0
Two for loops, add the first element in the first loop and the others in the second one while checking that value + tab[j] <= 0.8
, in some cases it works and in others, it's less than 0.8.
答案1
得分: 0
以下是翻译好的部分:
"Here's a recursive version." -> "这是一个递归版本。"
"We have base cases when the total is zero, and we return true
, when the list of values is empty, and we return false
. If the first number in the list is too large, we drop it and recur. Then we try using the first number, subtracting it from the target and recurring with the remaining numbers. If that doesn't work, we try dropping the first number and recurring with the same target." -> "当总和为零时,我们有基本情况,返回true
;当值的列表为空时,返回false
。如果列表中的第一个数字太大,我们会将其丢弃并进行递归。然后,我们尝试使用第一个数字,从目标中减去它并使用剩余的数字进行递归。如果这不起作用,我们尝试丢弃第一个数字并使用相同的目标进行递归。"
"const canSum = (t, [n, ...ns]) =>\n t == 0\n ? true\n : n === undefined\n ? false\n : n > t\n ? canSum (t, ns)\n : canSum (t - n, ns) || canSum (t, ns)" -> "const canSum = (t, [n, ...ns]) =>\n t == 0\n ? true\n : n === undefined\n ? false\n : n > t\n ? canSum (t, ns)\n : canSum (t - n, ns) || canSum (t, ns)"
"console .log (canSum (0.8, [0.4, 0.1, 0.4, 0.5]) //=> ${canSum (0.8, [0.4, 0.1, 0.4, 0.5])}
)" -> "console .log (canSum (0.8, [0.4, 0.1, 0.4, 0.5]) //=> ${canSum (0.8, [0.4, 0.1, 0.4, 0.5]}
"
"console .log (' ')" -> "console .log (' ')"
"console .log ('Trying 1 - 40 with [2, 3, 5, 7, 11, 13]')" -> "console .log ('尝试 1 - 40 与 [2, 3, 5, 7, 11, 13]')"
"console .log ('=======================================')" -> "console .log ('=======================================')"
"Array.from ({length: 40}, (, i) => i + 1)\n .forEach (n => console .log ([2, 3, 5, 7, 11, 13] ${canSum (n, [2, 3, 5, 7, 11, 13]) ? '*can*' : '*cannot*'} sum to ${n}
))" -> "Array.from ({length: 40}, (, i) => i + 1)\n .forEach (n => console .log ([2, 3, 5, 7, 11, 13] ${canSum (n, [2, 3, 5, 7, 11, 13]) ? '*can*' : '*cannot*'} sum to ${n}
))"
"We can write something similar to find the first set that works:" -> "我们可以编写类似的代码来查找第一个有效的集合:"
"const findSummands = (\n t, [n, ...ns], \n u = t > 0 && n != undefined && findSummands (t, ns), \n v = t > 0 && n != undefined && findSummands (t - n, ns)\n) => t == 0 ? [] : n === undefined ? undefined : u ? u : (v ? [n, ...v] : undefined)" -> "const findSummands = (\n t, [n, ...ns], \n u = t > 0 && n != undefined && findSummands (t, ns), \n v = t > 0 && n != undefined && findSummands (t - n, ns)\n) => t == 0 ? [] : n === undefined ? undefined : u ? u : (v ? [n, ...v] : undefined)"
"console .log (findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) //=> [${findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) .join(', ')}]
)" -> "console .log (findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) //=> [${findSummands (0.8, [0.4, 0.1, 0.4, 0.5]).join(', ')}]
"
"console .log (' ')" -> "console .log (' ')"
"console .log ('Trying 1 - 40 with [2, 3, 5, 7, 11, 13]')" -> "console .log ('尝试 1 - 40 与 [2, 3, 5, 7, 11, 13]')"
"console .log ('=======================================')" -> "console .log ('=======================================')"
"Array.from ({length: 40}, (, i) => i + 1) .forEach (n => {\n const summands = findSummands (n, [2, 3, 5, 7, 11, 13])\n console .log (\n summands ? ${n} = ${summands .join (' + ')}
: ${n} (no answer found)
\n )\n})" -> "Array.from ({length: 40}, (, i) => i + 1) .forEach (n => {\n const summands = findSummands (n, [2, 3, 5, 7, 11, 13])\n console .log (\n summands ? ${n} = ${summands .join (' + ')}
: ${n} (no answer found)
\n )\n})"
英文:
Here's a recursive version.
We have base cases when the total is zero, and we return true
, when the list of values is empty, and we return false
. If the first number in the list is too large, we drop it and recur. Then we try using the the first number, subtracting it from the target and recurring with the remaining numbers. If that doesn't work, we try dropping the first number and recurring with the same target.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const canSum = (t, [n, ...ns]) =>
t == 0
? true
: n === undefined
? false
: n > t
? canSum (t, ns)
: canSum (t - n, ns) || canSum (t, ns)
console .log (`canSum (0.8, [0.4, 0.1, 0.4, 0.5]) //=> ${canSum (0.8, [0.4, 0.1, 0.4, 0.5])}`)
console .log (' ')
console .log (`Trying 1 - 40 with [2, 3, 5, 7, 11, 13]`)
console .log (`=======================================`)
Array.from ({length: 40}, (_, i) => i + 1)
.forEach (n => console .log (`[2, 3, 5, 7, 11, 13] ${canSum (n, [2, 3, 5, 7, 11, 13]) ? '*can*' : '*cannot*'} sum to ${n}`))
<!-- language: lang-css -->
.as-console-wrapper {max-height: 100% !important; top: 0}
<!-- end snippet -->
We can write something similar to find the first set that works:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const findSummands = (
t, [n, ...ns],
u = t > 0 && n != undefined && findSummands (t, ns),
v = t > 0 && n != undefined && findSummands (t - n, ns)
) => t == 0 ? [] : n === undefined ? undefined : u ? u : (v ? [n, ...v] : undefined)
console .log (`findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) //=> [${findSummands (0.8, [0.4, 0.1, 0.4, 0.5]) .join(', ')}]`)
console .log (' ')
console .log (`Trying 1 - 40 with [2, 3, 5, 7, 11, 13]`)
console .log (`=======================================`)
Array.from ({length: 40}, (_, i) => i + 1) .forEach (n => {
const summands = findSummands (n, [2, 3, 5, 7, 11, 13])
console .log (
summands ? `${n} = ${summands .join (' + ')}` : `${n} (no answer found)`
)
})
<!-- language: lang-css -->
.as-console-wrapper {max-height: 100% !important; top: 0}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论