-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_kyu_Beginner_Series_#3_Sum_of_Numbers.py
50 lines (35 loc) · 1.44 KB
/
7_kyu_Beginner_Series_#3_Sum_of_Numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""7 kyu Beginner Series #3 Sum of Numbers
Description:
Given two integers a and b, which can be positive or negative,
find the sum of all the integers between and including them
and return it. If the two numbers are equal return a or b.
Note: a and b are not ordered!
Examples (a, b) --> output (explanation)
(1, 0) --> 1 (1 + 0 = 1)
(1, 2) --> 3 (1 + 2 = 3)
(0, 1) --> 1 (0 + 1 = 1)
(1, 1) --> 1 (1 since both are same)
(-1, 0) --> -1 (-1 + 0 = -1)
(-1, 2) --> 2 (-1 + 0 + 1 + 2 = 2)
Fundamentals
Algorithms"""
# 1. oneliner, sum(), range(), for loop, min(), max()
# Here I didn't know how range exactly works.
def get_sum(a, b):
return sum(x for x in range(min(a, b), max(a, b) + 1))
# 2. oneliner, sum(), range(), min(), max()
# almost same, but no for loop. range() - returns a iterable class 'range'.
def get_sum2(a, b):
return sum(range(min(a, b), max(a, b) + 1))
# test zone
import pytest
@pytest.mark.parametrize('a, b, expected_result', [(0, 1, 1),
(0, 0, 0),
(-50, 0, -1275)])
def test_example(a, b, expected_result):
assert get_sum(a, b) == expected_result
@pytest.mark.parametrize('a, b, expected_result', [(0, 1, 1),
(0, 0, 0),
(-50, 0, -1275)])
def test_example2(a, b, expected_result):
assert get_sum2(a, b) == expected_result