# Calculate the Sum of the two lowest positive integers in Python

## The challenge

Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 positive integers. No floats or non-positive integers will be passed.

For example, when an array is passed like `[19, 5, 42, 2, 77]`, the output should be `7`.

`[10, 343445353, 3453445, 3453545353453]` should return `3453455`.

## The solution in Python code

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```def sum_two_smallest_numbers(numbers):
return sum(sorted(numbers)[:2])
```Code language: Python (python)```

Option 2:

``````def sum_two_smallest_numbers(num_list):
num_list.sort()
return num_list[0] + num_list[1]
```Code language: Python (python)```

Option 3:

``````sum_two_smallest_numbers = lambda A: sum( sorted( filter( lambda x:x>0, A) )[:2] )
```Code language: Python (python)```

## Test cases to validate our solution

``````import test
from solution import sum_two_smallest_numbers

@test.describe("Fixed Tests")
def fixed_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
test.assert_equals(sum_two_smallest_numbers([5, 8, 12, 18, 22]), 13)
test.assert_equals(sum_two_smallest_numbers([7, 15, 12, 18, 22]), 19)
test.assert_equals(sum_two_smallest_numbers([25, 42, 12, 18, 22]), 30)
```Code language: Python (python)```
Tags:
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments