当前位置:   article > 正文

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

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])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

测试一下代码

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

其中打印的输出结果是:

 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/490112
推荐阅读
相关标签
  

闽ICP备14008679号