kata题目

来源:互联网 发布:linux做网站需要环境 编辑:程序博客网 时间:2024/05/22 02:13

My little sister came back home from school with the following task:given a squared sheet of paper she has to cut it in pieceswhich, when assembled, give squares the sides of which forman increasing sequence of numbers.At the beginning it was lot of fun but little by little we were tired of seeing the pile of torn paper.So we decided to write a program that could help us and protects trees.

Examples

decompose(11) must return [1,2,4,10]. Note that there are actually two ways to decompose 11²,11² = 121 = 1 + 4 + 16 + 100 = 1² + 2² + 4² + 10² but don't return[2,6,9], since 9 is smaller than 10.

For decompose(50) don't return [1, 1, 4, 9, 49] but [1, 3, 5, 8, 49] since [1, 1, 4, 9, 49]doesn't form a strictly increasing sequence.


python:


def decompose(n):    def recurse(s,i):        if s<0:            return None        if s == 0:            return []        for j in xrange(i - 1,0,-1):            sub = recurse(s-j**2,j)            if sub != None:                return sub+[j]    return recurse(n**2,n)

原创粉丝点击