목차
Question
When two integers a and b are given, return the sum of integers between a and b.
For example, when a is 3 and b is 5, 3 + 4+ 5 = 12, then return 12.
Limitation
- If a and b are equal, return one of them.
- a and b is more than or equal to -10,000,000 and less than or equal to 10,000,000.
- a and b will be given regardless of size.
Example of I/O
| a | b | return |
| 3 | 5 | 12 |
| 3 | 3 | 3 |
| 5 | 3 | 12 |
My Solution
def solution(a, b):
answer = 0
if a == b:
answer = a
elif a < b:
for x in range(a, b + 1):
answer += x
else:
for x in range(b, a + 1):
answer += x
return answer
Explanation
Reference
코딩테스트 연습 - 두 정수 사이의 합
두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요. 예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다. 제한 조건 a와 b가 같은 경우
programmers.co.kr
'Problem solving > Algorithms' 카테고리의 다른 글
| [LeetCode] 1480. Running Sum of 1d Array (Python) (0) | 2021.01.09 |
|---|---|
| An array of divisible numbers (나누어 떨어지는 숫자 배열) (0) | 2020.09.10 |
| Finding prime numbers (소수 찾기) (0) | 2020.08.28 |
| Kth Number (K번째 수) (0) | 2020.08.27 |
| Mock Test Answers (모의고사) (0) | 2020.08.26 |