|
| 1 | +"""Kata - Exercise in Summing |
| 2 | +
|
| 3 | +completed at: 2024-08-21 17:33:03 |
| 4 | +by: Jakub Červinka |
| 5 | +
|
| 6 | +Your task is to finish two functions, `minimumSum` and `maximumSum`, that take 2 parameters: |
| 7 | +
|
| 8 | +- `values`: an array of integers with an arbitrary length; may be positive and negative |
| 9 | +- `n`: how many integers should be summed; always 0 or bigger |
| 10 | +
|
| 11 | +### Example: |
| 12 | +
|
| 13 | +```javascript |
| 14 | +var values = [5, 4, 3, 2, 1]; |
| 15 | +minimumSum(values, 2); // should return 1+2 = 3 |
| 16 | +maximumSum(values, 3); // should return 3+4+5 = 12 |
| 17 | +``` |
| 18 | +
|
| 19 | +```coffeescript |
| 20 | +values = [5, 4, 3, 2, 1] |
| 21 | +minimumSum values, 2 # should return 1+2 = 3 |
| 22 | +maximumSum values, 3 # should return 3+4+5 = 12 |
| 23 | +``` |
| 24 | +
|
| 25 | +```python |
| 26 | +values = [5, 4, 3, 2, 1]; |
| 27 | +minimum_sum(values, 2) #should return 1 + 2 = 3 |
| 28 | +maximum_sum(values, 3) #should return 3 + 4 + 5 = 12 |
| 29 | +``` |
| 30 | +
|
| 31 | +```haskell |
| 32 | +minimumSum [1..5] 2 `shouldBe` 1 + 2 |
| 33 | +minimumSum [1..5] 3 `shouldBe` 1 + 2 + 3 |
| 34 | +maximumSum [1..5] 2 `shouldBe` 4 + 5 |
| 35 | +maximumSum [1..5] 3 `shouldBe` 3 + 4 + 5 |
| 36 | +``` |
| 37 | +
|
| 38 | +All values given to the functions will be integers. Also take care of the following special cases: |
| 39 | +
|
| 40 | +- if `values` is empty, both functions should return 0 |
| 41 | +- if `n` is 0, both functions should also return 0 |
| 42 | +- if `n` is larger than `values`'s length, use the length instead. |
| 43 | +""" |
| 44 | + |
| 45 | +def minimum_sum(values, n): |
| 46 | + return sum(sorted(values)[:n]) |
| 47 | + |
| 48 | +def maximum_sum(values, n): |
| 49 | + if n == 0: return 0 |
| 50 | + return sum(sorted(values)[-n:]) |
0 commit comments