Passed
Push — master ( 8229a7...291868 )
by Ken M.
01:19 queued 22s
created

stick_sawing   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 7

3 Functions

Rating   Name   Duplication   Size   Complexity  
A is_consecutive() 0 7 2
A triangular_number() 0 2 1
A checkio() 0 14 4
1
from math import sqrt
2
3
4
def triangular_number(n):
5
    return n * (n + 1) / 2
6
7
8
def is_consecutive(number_list, j):
9
    start_index = number_list.index(j[0])
10
    end_index = number_list.index(j[-1])
11
    if start_index + len(j) - 1 == end_index:
12
        return True
13
    else:
14
        return False
15
16
17
def checkio(number):
18
    n = sqrt(number * 2)
19
    number_list = [
20
        triangular_number(i)
21
        for i in range(1, int(n) + 1)
22
        if triangular_number(i) < number
23
    ]
24
    length = len(number_list)
25
    while length > 1:
26
        for i in range(len(number_list) - length + 1):
27
            if sum(number_list[i : i + length]) == number:
28
                return number_list[i : i + length]
29
        length -= 1
30
    return []
31
32
33
# These "asserts" using only for self-checking and not necessary for
34
# auto-testing
35
if __name__ == '__main__':
36
    assert checkio(64) == [15, 21, 28], "1st example"
37
    assert checkio(371) == [36, 45, 55, 66, 78, 91], "1st example"
38
    assert checkio(225) == [105, 120], "1st example"
39
    assert checkio(882) == [], "1st example"
40