python使用tree计算分区数

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())

第一个问题:

参考链接:(3条消息) 【CS61A】count_partition(整数划分)_Sitetampo的博客-CSDN博客

(3条消息) CS61A 18sp -- Lecture8 (Tree Recursion) 笔记_吾乃皮皮兽啊的博客-CSDN博客

看出一种递推方法(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 Recursive Functions (composingprograms.com)

1.7.5 Example: Partitions文档解读

Video: Show Hide

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 in increasing order. For example, the number of partitions of 6 using parts up to 4 is 9.

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

  1. 6 = 2 + 4

  1. 6 = 1 + 1 + 4

  1. 6 = 3 + 3

  1. 6 = 1 + 2 + 3

  1. 6 = 1 + 1 + 1 + 3

  1. 6 = 2 + 2 + 2

  1. 6 = 1 + 1 + 2 + 2

  1. 6 = 1 + 1 + 1 + 1 + 2

  1. 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 equals

  1. the number of ways to partition n-m using integers up to m, and

  1. 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.

  1. There are 0 ways to partition a negative n.

  1. There are 0 ways to partition any n greater than 0 using parts of size 0 or less.

有一种分区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作为参数,并将两次调用的结果相加

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈都婆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值