Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Straightforward solution in Clear category for Saw the Stick by MackM
def Get_Triangle_Numbers(Maximum_Number):
'''
Returns a list of all Triangle numbers less than or equal to Maximum_Number.
'''
Triangle_Numbers = []
Triangle_Number = 1
Next_Addition = 2
while Triangle_Number <= Maximum_Number:
Triangle_Numbers.append(Triangle_Number)
Triangle_Number += Next_Addition
Next_Addition += 1
return Triangle_Numbers
def checkio(Number):
Triangle_Numbers = Get_Triangle_Numbers(Number)
Start_Index = 0
Stop_Index = len(Triangle_Numbers)
Running_Total = 0
while True:
Running_Total = 0
if Start_Index == Stop_Index:
return []
for Index in range(Start_Index, Stop_Index):
Running_Total += Triangle_Numbers[Index]
if Running_Total == Number:
return Triangle_Numbers[Start_Index:Index+1]
if Running_Total > Number:
break
Start_Index += 1
#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 7, 2014