利用Flowchart.fun快速可视化递归过程

主要目的

对于算法初学者来说,能将算法运算过程可视化,则将对算法过程有一个更深刻的理解。
对我来说,在开始学习算法的时候就想有一个工具能快速的展现算法的运算过程。正好前段时间看到了Flowchart.fun觉的很有意思。决定将其运用在算法上。

Flowchart.fun使用

Flowchart.fun是一个通过文本录入快速制作生成流程图的小应用。一行代表一个流程块。缩进代表子节点;

实战运行手册6期——流程图小工具flowchart-fun( https://zhuanlan.zhihu.com/p/359466794)

展现显示结果

Flowchart.fun生成的计算过程

代码展示

简单的示例

class Solution():
    """
    问题描述:给定数组s以及目标target,在数组的元素前面添加+或者-,使其等于target,并且每个元素都要用上。求有多少种方法
    example:s=[1,2,3,4,5],target=3,1-2+3-4+5=3的方法数
    """
    def __init__(self,i):
        self.i = i
    def aaa(self,s,target):
        return self.process(s,0,target)
        
    def process(self,s,index,rest):
        
        print(" "*index,"index is ",index,"rest is ", rest,"order is ", self.i)
        self.i=self.i+1
        if rest==0 and index==len(s):
            return 1
        elif rest!=0 and index==len(s):
            return 0
        else:
            
            return self.process(s,index+1,rest-s[index])+self.process(s,index+1,rest+s[index])

测试一下代码

sulution = Solution(0)
sulution.aaa([1,2,3],6)

其中打印的输出结果是:

 index is  0 rest is  6 order is  0
  index is  1 rest is  5 order is  1
   index is  2 rest is  3 order is  2
    index is  3 rest is  0 order is  3
    index is  3 rest is  6 order is  4
   index is  2 rest is  7 order is  5
    index is  3 rest is  4 order is  6
    index is  3 rest is  10 order is  7
  index is  1 rest is  7 order is  8
   index is  2 rest is  5 order is  9
    index is  3 rest is  2 order is  10
    index is  3 rest is  8 order is  11
   index is  2 rest is  9 order is  12
    index is  3 rest is  6 order is  13
    index is  3 rest is  12 order is  14

将上面打印的结果复制到Flowchart.fun中就可以看到整个的计算流程。
大家如果有更好的方法,欢迎交流讨论。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值