#16 - Sums Of Parts CodeWars Kata (6 Kyu) - DEV Community
Maybe your like
Instructions
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 function parts_sums (or its variants in other languages) will take as parameter a list ls and 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]
Notes Take a look at performance: some lists have thousands of elements. Please ask before translating.
My solution:
function partsSums(ls) { let result = [0] for(const n in ls.reverse()){ result.push(ls[n]+result[n]); } return result.reverse() } Enter fullscreen mode Exit fullscreen modeExplanation
First I declarated a variable result that is an array that contains a 0 in it
let result = [0]
Then after that I used a for In loop to iterate the ls array but reversed, in every iteration I would push to the result array, the result of the sum of the n element of ls and the n element of the result array.
for(const n in ls.reverse()){ result.push(ls[n]+result[n]); }
After that I would only return the result array but reversed
return result.reverse()
Comment how would you solve this kata and why? 👇🤔
My Github My twitter Solve this Kata
Tag » Codewars 6 Kyu Solutions Java
-
Codewars Solutions (Java) - GitHub
-
Codewars-solutions · GitHub Topics
-
6 Kyu - Solutions - Codewars
-
Hi, Everybody. I Am Solving A 6 Kyu Problem On Code Wars And My ...
-
Daily Java “Day 29” Codewars (6 Kyu) Multiples Of 3 And 5
-
#42 - How Many Pages In A Book? - Codewars Kata (6 Kyu) - DEV ...
-
Do You Expect A Junior Programmer To Be Able To Solve 6 Kyu ... - Quora
-
Codewars-javascript · GitHub Topics
-
Sans Titre
-
Codewars · GitHub Topics
-
Codewars Challenge - Java - Episode 1 - YouTube
-
How To Solve A 1 Kyu Kata On Codewars (or Any Difficult ... - Medium
-
Codewars-解决方案github, 对代码战javascript 的总和 ... - 免费编程教程
-
Does Codewars Get Better? : R/learnprogramming - Reddit