나는 이렇게 학습한다/Algorithm & SQL

Sum of a sequence

daco2020 2022. 8. 10. 18:44
반응형

Your task is to make function, which returns the sum of a sequence of integers.

The sequence is defined by 3 non-negative values: begin, end, step (inclusive).

If begin value is greater than the end, function should returns 0

Examples

2,2,2 --> 2
2,6,2 --> 12 (2 + 4 + 6)
1,5,1 --> 15 (1 + 2 + 3 + 4 + 5)
1,5,3  --> 5 (1 + 4)

 

 

Solotion:

def sequence_sum(begin_number, end_number, step):
    return sum(range(begin_number, end_number+1, step))

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Maximum subarray sum  (0) 2022.08.12
List Filtering  (0) 2022.08.11
Remove anchor from URL  (0) 2022.08.09
Find the unique number  (0) 2022.08.08
Fix string case  (2) 2022.08.07