Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
simple solution in Clear category for Saw the Stick by masamish
from functools import reduce
def getTriangularNumbersLessThan(N):
import math
maxk = int((-1+math.sqrt(1+8*N))//2)
return [k*(k+1)//2 for k in range(0, maxk + 1)]
def checkio(number):
# get a list of the triangular numbers
trinum = getTriangularNumbersLessThan(number)
# make candidates
candidates = [trinum[x:y+1] for x in range(1, len(trinum)) \
for y in range(1, len(trinum)) if x <= y]
# print(candidates)
# filter and select
return reduce(\
lambda _, r: r if len(r)>len(_) else _,
filter(lambda s: sum(s)==number, candidates),
[] )
#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"
March 25, 2015