반응형
Let us consider this example (array written in general format):
ls = [0, 1, 3, 6, 10]
Its following parts:
ls = [0, 1, 3, 6, 10]
ls = [1, 3, 6, 10]
ls = [3, 6, 10]
ls = [6, 10]
ls = [10]
ls = []
The corresponding sums are (put together in a list):[20, 20, 19, 16, 10, 0]
The functionparts_sums(or its variants in other languages) will take as parameter a listlsand return a list of the sums of its parts as defined above.
Other Examples:
ls = [1, 2, 3, 4, 5, 6]
parts_sums(ls) -> [21, 20, 18, 15, 11, 6, 0]
ls = [744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]
parts_sums(ls) -> [10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0]
function partsSums(ls) {
let sum = ls.reduce((sum, n) => sum + n, 0)
res = [sum]
for (let i = 1; i <= ls.length; i++){
sum -= ls[i-1]
res.push(sum )
}
return res
}
반응형
'개발 > Javascript' 카테고리의 다른 글
모던 자바스크립트는 언제부터일까? (0) | 2021.07.05 |
---|---|
[기초] javascript 구성요소 (0) | 2021.07.02 |
[기초] javascript Scope (0) | 2021.06.17 |
[알고리즘] Find The Parity Outiler (0) | 2021.05.20 |
완전 쌩 초짜가 FE개발 공부할때 읽기 좋은책 (0) | 2021.04.05 |