Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Saw the Stick by aya.kanazawa
def checkio(number):
t = []
for n in range(1, number//2):
if int(n * (n+1) / 2) > number:
break
t.append(int(n * (n+1) / 2))
def try_slice(slice_num):
for start in range(len(t)-slice_num):
if sum(t[start : start + slice_num]) == number:
return t[start : start + slice_num]
return None
for slice_num in range(len(t), 1, -1):
array_or_none = try_slice(slice_num)
if array_or_none:
return array_or_none
return []
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(64) == [15, 21, 28], "1st example"
assert checkio(371) == [36, 45, 55, 66, 78, 91], "1st example"
assert checkio(225) == [105, 120], "1st example"
assert checkio(882) == [], "1st example"
July 5, 2019