赞
踩
对于算法初学者来说,能将算法运算过程可视化,则将对算法过程有一个更深刻的理解。
对我来说,在开始学习算法的时候就想有一个工具能快速的展现算法的运算过程。正好前段时间看到了Flowchart.fun觉的很有意思。决定将其运用在算法上。
Flowchart.fun是一个通过文本录入快速制作生成流程图的小应用。一行代表一个流程块。缩进代表子节点;
实战运行手册6期——流程图小工具flowchart-fun( https://zhuanlan.zhihu.com/p/359466794)
简单的示例
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中就可以看到整个的计算流程。
大家如果有更好的方法,欢迎交流讨论。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。