물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Sum of Numbers

BEstyle 2022. 10. 4. 12:48

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)

def get_sum(a,b):
    ans=0
    for i in range(min(a,b),max(a,b)+1):
        ans+=i
    return ans

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Find the next perfect square!  (0) 2022.10.05
Equals Sides Of An Array  (0) 2022.10.05
Your order, please  (0) 2022.10.04
Credit Card Mask  (0) 2022.10.04
Jaden Casing Strings  (0) 2022.10.04