見習村09 - Sums of Parts

09 - Sums of Parts

Don’t say so much, just coding…

Instruction

Let us consider this example (array written in general format):

ls = [0, 1, 3, 6, 10]

Its following parts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  ls = [0, 1, 3, 6, 10]
ls = [1, 3, 6, 10]
ls = [3, 6, 10]
ls = [6, 10]# 09 - Sums of Parts

Don't say so much, just coding...

## Instruction

Let us consider this example (array written in general format):

`ls = [0, 1, 3, 6, 10]`

Its following parts:

```javascript
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:

1
2
3
4
5
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]

Ruby

Init

1
2
3
def parts_sums(ls)
#...
end

Sample Testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
describe("parts_sums") do
def dotest(ls, expect)
actual = parts_sums(ls)
Test.assert_equals(actual, expect, "")
end

it("Basic tests") do
dotest([], [0])
dotest([0, 1, 3, 6, 10], [20, 20, 19, 16, 10, 0])
dotest([1, 2, 3, 4, 5, 6], [21, 20, 18, 15, 11, 6, 0])
dotest([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358],
[10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0])
end
end

Javascript

Init

1
2
3
function partsSums(ls) {
// your code
}

Sample Testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold = 0;

describe("partsSums",function() {

function dotest(ls, expect) {
let actual = partsSums(ls);
assert.deepEqual(actual, expect);
}

it("Basic tests",function() {
dotest([], [0]);
dotest([0, 1, 3, 6, 10], [20, 20, 19, 16, 10, 0]);
dotest([1, 2, 3, 4, 5, 6], [21, 20, 18, 15, 11, 6, 0]);
dotest([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358],
[10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0]);
})})

Thinking

想法(1): 這題應該來算是數學題,傳進來的值(陣列)會先加總,並且減掉陣列第一項
想法(2): 直至把所有值都扣除完,會得到一個新的陣列,並帶有每次扣除的總和

https://ithelp.ithome.com.tw/upload/images/20200924/20120826GsGyEU0V2s.jpg
圖片來源:Unsplash Christin Hume

Hint & Reference

  • Ruby
  • JavaScript

之前前幾篇應該都介紹到可以用到的方法了,接下來的十天也開始挑戰更高一個階級的題目ouo

Solution

Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Solution 1
def parts_sums(ls)
sum = ls.sum
result = [sum]
(0...ls.size).each { |index|
result << result.last - ls[index]
}
result
end

# Solution 2
def parts_sums(ls)
sum = ls.sum
[sum] + ls.map{ |el| sum -= el }
end

Javascript

1
2
3
4
5
6
7
8
9
10
// Solution 1
function partsSums(ls) {
var sum = ls.reduce((a, b) => a + b, 0);
var result = [sum];

for(i = 0; i < ls.length; i++){
result.push((result[result.length-1]) - ls[i]);
};
return result
}

ls = [10]
ls = []

1
2
3
4
5
6
7
8
9
10
11
12
13

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:

```javascript
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]

Ruby

Init

1
2
3
def parts_sums(ls)
#...
end

Sample Testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
describe("parts_sums") do
def dotest(ls, expect)
actual = parts_sums(ls)
Test.assert_equals(actual, expect, "")
end

it("Basic tests") do
dotest([], [0])
dotest([0, 1, 3, 6, 10], [20, 20, 19, 16, 10, 0])
dotest([1, 2, 3, 4, 5, 6], [21, 20, 18, 15, 11, 6, 0])
dotest([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358],
[10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0])
end
end

Javascript

Init

1
2
3
function partsSums(ls) {
// your code
}

Sample Testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold = 0;

describe("partsSums",function() {

function dotest(ls, expect) {
let actual = partsSums(ls);
assert.deepEqual(actual, expect);
}

it("Basic tests",function() {
dotest([], [0]);
dotest([0, 1, 3, 6, 10], [20, 20, 19, 16, 10, 0]);
dotest([1, 2, 3, 4, 5, 6], [21, 20, 18, 15, 11, 6, 0]);
dotest([744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358],
[10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0]);
})})

Thinking

想法(1):
想法(2):
想法(3):

圖片—
圖片來源:Unsplash Roman Bozhko

Hint & Reference

Solution

Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Solution 1
def parts_sums(ls)
sum = ls.sum
result = [sum]
(0...ls.size).each { |index|
result << result.last - ls[index]
}
result
end

# Solution 2
def parts_sums(ls)
sum = ls.sum
[sum] + ls.map{ |el| sum -= el }
end

Javascript

1
2
3
4
5
6
7
8
9
10
// Solution 1
function partsSums(ls) {
var sum = ls.reduce((a, b) => a + b, 0);
var result = [sum];

for(i = 0; i < ls.length; i++){
result.push((result[result.length-1]) - ls[i]);
};
return result
}