python使用tree计算分区数

可以使用OS模块中的os.popen

import os
def count_partitions():
    result = os.popen("df -h").read()
    lines = result.split("\n")
    return len(lines) - 1
print("分区数量为:", count_partitions())

第一个问题:

看出一种递推方法(Recursive calls)

def count_partitions(n,m):
    if n==0:
        return 1
    elif n<0:
        return 0
    elif m==0:
        return 0
    else:
        with_m=count_partitions(n-m,m)
        without_m=count_partitions(n,m-1)
        return with_m + without_m
    
result=count_partitions(5,5)
print(result)

代码解释:

定义count_partitions(计算分区)的函数,两个参数n和m,并返回将n分成小于等于m的部分的方法数。函数通过使用递归调用实现。它将分成包含m和不包含m两种情况进行递归调用,并最终将结果相加。最后,函数返回的结果被存储在result变量中,打印。

第二部分

1.7.5 Example: Partitions文档解读

Video:

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m

正整数n的分区数,使用大小为m的部分,是n可以按递增顺序表示为m以下正整数部分的和的方法的数量。例如,使用最多4个部件的分区为6,分区数为9。

  1. 6 = 2 + 4
  2. 6 = 1 + 1 + 4
  3. 6 = 3 + 3
  4. 6 = 1 + 2 + 3
  5. 6 = 1 + 1 + 1 + 3
  6. 6 = 2 + 2 + 2
  7. 6 = 1 + 1 + 2 + 2
  8. 6 = 1 + 1 + 1 + 1 + 2
  9. 6 = 1 + 1 + 1 + 1 + 1 + 1

We will define a function count_partitions(n, m) that returns the number of different partitions of n using parts up to m. This function has a simple solution as a tree-recursive function, based on the following observation:

The number of ways to partition n using integers up to m

  1. the number of ways to partition n-m using integers up to m, and
  2. the number of ways to partition n using integers up to m-1.
To see why this is true, observe that all the ways of partitioning n can be divided into two groups: those that include at least one m and those that do not. Moreover, each partition in the first group is a partition of n-m, followed by m added at the end. In the example above, the first two partitions contain 4, and the rest do not.
Therefore, we can recursively reduce the problem of partitioning n using integers up to m into two simpler problems: (1) partition a smaller number n-m, and (2) partition with smaller components up to m-1.

要了解为什么是这样,观察所有划分n的方法都可以分为两组:至少包括一个m的组和不包括m的组。而且,第一组中的每个分区都是n-m个分区,最后加上m个分区。在上面的示例中,前两个分区包含4,其余分区不包含4。
因此,我们可以递归地将使用小于m的整数划分n的问题简化为两个更简单的问题:(1)划分较小的数字n-m,(2)划分小于m-1的较小分量。

解释:通过划分将n分为至少包含一个m的分量和不包含m的分量,需要指定以下基本情况:

To complete the implementation, we need to specify the following base cases:

  1. There is one way to partition 0: include no parts.
  2. There are 0 ways to partition a negative n.
  3. There are 0 ways to partition any n
有一种分区0的方法: 不包括任何部分。
分割负 n 的方法有0种。
使用大小为0或更小的部分对任何大于0的 n 进行分区的方法为0

1.2函数:计算使用最多m的部件划分n的方法

>>> defcount_partitions(n,m): """Count the ways to partition n using parts up to m.""" ifn==0: return1 elifn<0: return0 elifm==0: return0 else: returncount_partitions(n-m,m)+count_partitions(n,m-1)

def count_partitions(n,m):
    if n==0:
        return 1
    elif n<0:
        return 0
    elif m==0:
        return 0
    else:
        with_m=count_partitions(n-m,m)
        without_m=count_partitions(n,m-1)
        return with_m + without_m

1.3就是用哪个函数计算可以的方法

result=count_partitions(5,5)
print(result)
>>> count_partitions(6,4)9>>> count_partitions(5,5)7>>> count_partitions(10,10)42>>> count_partitions(15,15)176>>> count_partitions(20,20)627
We can think of a tree-recursive function as exploring different possibilities. In this case, we explore the possibility that we use a part of size m and the possibility that we do not. The first and second recursive calls correspond to these possibilities.
Implementing this function without recursion would be substantially more involved. Interested readers are encouraged to try.





##树递归函数
我们可以把树递归函数看作是探索不同的可能性。在这种情况下,我们探讨了使用大小为m的一部分的可能性,以及不使用的可能性。第一个和第二个递归调用对应于这些可能性。
在不使用递归的情况下实现这个函数会更加复杂。有兴趣的读者可以尝试一下。

递归函数计算正整数n的分割数量,并且可以使用小于等于m的部分进行分割。分割是指将n表示为由递增的正整数部分的和的形式。递归函数实现的思路是:如果n为0,则返回1;如果n为负数,则返回0;如果m为0或小于0,则返回0;否则,递归调用函数,并将n-m和m-1作为参数,并将两次调用的结果相加