KATA问题:广场成广场保护树

时间:2019-01-07 17:55:51

标签: python algorithm

我自己无法解决以下问题,因此我搜索并找到了有效的算法。有人可以给我解释一下思维过程,数学或该算法背后的内容吗?

kata:https://www.codewars.com/kata/square-into-squares-protect-trees/train/python

算法(https://github.com/Peter-Liang/CodeWars-Python/blob/master/solutions/Square_into_Squares_Protect_trees.py):

def decompose(n):
    goal = 0
    result = [n]
    while result:
        current = result.pop()
        goal += current ** 2
        for i in range(current - 1, 0, -1):
            if goal - (i ** 2) >= 0:
                goal -= i ** 2
                result.append(i)
                if goal == 0:
                    result.sort()
                    return result
    return None
  

edit:我是Python的新手,在理解代码时遇到了一些麻烦。现在对我来说很清楚。

2 个答案:

答案 0 :(得分:0)

让我们让程序自我解释:

def decompose(n):
    goal = 0
    print "Adding n, %s, to sequence.\n" % (n)
    result = [n]
    while result:
        current = result.pop()
        print "The last number, %s, wasn't helpful. Removing it from sequence and adding it back to `goal`" % (current)
        print "Trying lower numbers now.\n" if current - 1 > 0 else "\n"

        goal += current ** 2
        for i in range(current - 1, 0, -1):
            print "Trying %s" % (i)
            if goal - (i ** 2) >= 0:
                goal -= i ** 2
                result.append(i)
                "This number, %s, might work. Goal is not below zero. Adding it to sequence and subtracting from `goal`." % (i)
                if goal == 0:
                    result.sort()
                    print "\nFound result, %s." % (result)
                    return result
            else:
              print "This number, %s, is too big here. Continuing." % (i)
    return None

产生:

> decompose(10)

Adding n, 10, to sequence.

The last number, 10, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.

Trying 9
Trying 8
This number, 8, is too big here. Continuing.
Trying 7
This number, 7, is too big here. Continuing.
Trying 6
This number, 6, is too big here. Continuing.
Trying 5
This number, 5, is too big here. Continuing.
Trying 4
Trying 3
This number, 3, is too big here. Continuing.
Trying 2
This number, 2, is too big here. Continuing.
Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`


The last number, 4, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.

Trying 3
Trying 2
Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`


The last number, 2, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.

Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`


The last number, 3, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.

Trying 2
Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`


The last number, 2, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.

Trying 1
The last number, 1, wasn't helpful. Removing it from sequence and adding it back to `goal`


The last number, 9, wasn't helpful. Removing it from sequence and adding it back to `goal`
Trying lower numbers now.

Trying 8
Trying 7
This number, 7, is too big here. Continuing.
Trying 6

Found result, [6, 8].
=> [6, 8]

答案 1 :(得分:0)

这可能是 js 的最佳解决方案,尽管如此简洁可能不利于理解。但它有效!

export class G964 {
  public static decompose = (n) => {
    let res = [n];
    while(res[0]){
      let area = n**2;
      let lim = res[res.length-1];
      res.pop();
      res.forEach(x => area -= x**2);
      for(var i=lim-1; i>0; i--){
        if(area - i**2 >= 0){
          res.push(i);
          area -= i**2;
          if(area === 0) return res.reverse();
        }
      }
    }
    return null;
  }
}