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 s7even
def checkio(data):
triangular = []
n = 0
i = 1
#make a list of Triangular numbers smaller than data
while n < data:
n += i
i += 1
if n < data:
triangular.append(n)
#Test all the numbers start from the longest possibility
for j in range(len(triangular),0,-1):
i = 0
while i+j < len(triangular):
if sum(triangular[i:i+j+1]) == data:
return triangular[i:i+j+1]
i += 1
return []
#def checkio(number):
# return [number // 2, number - number // 2]
#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"
Feb. 6, 2014
Comments: