02 2014 档案

摘要:class Stack(object): """ A class to hold arguements and state data. """ def __init__(self,**kwargs): self.__dict__.update(kwargs) def __repr__(self): extra = "|i:%s"%self.i if hasattr(self,'i') else '' return "n:%s|stage:%s%s"%(self.n 阅读全文
posted @ 2014-02-27 23:46 LisPythoniC 阅读(269) 评论(0) 推荐(0) 编辑
摘要:def hanoi_recur(n,reverse=True): if n==1: return 1,1 possible=[] iter_range=range(n-1,0,-1) if reverse else range(1,n) for i in iter_range: _, min_v = hanoi_recur(n-i,reverse) possible.append((i,2*min_v+2**i-1)) return min(possible,key=lambda x:x[1])如上所示,hanoi_rec... 阅读全文
posted @ 2014-02-27 23:39 LisPythoniC 阅读(255) 评论(0) 推荐(0) 编辑
摘要:class Stack(object): def __init__(self,**kwargs): self.__dict__.update(kwargs) def __str__(self): return '|'.join( ['%s:%s'%(k,getattr(self,k)) for k in sorted(self.__dict__)]) __repr__ = __str__def fab(n): if n==1 or n==2: return 1 return f... 阅读全文
posted @ 2014-02-26 17:41 LisPythoniC 阅读(293) 评论(0) 推荐(0) 编辑
摘要:摘自Handling Arbitrary StructuresOn the other hand, recursion—or equivalent explicit stack-based algorithms we’ll meetshortly—can be required to traverse arbitrarily shaped structures. As a simple exampleof recursion’s role in this context, consider the task of computing the sum of all thenumbers in a 阅读全文
posted @ 2014-02-23 15:25 LisPythoniC 阅读(446) 评论(0) 推荐(0) 编辑
摘要:def moves_three(n, a=0, b=1, c=2): '''give a int -> return a list ''' if n==1: return ((a,c),) return moves_three(n-1,a,c,b)+\ ((a,c),)+\ moves_three(n-1,b,a,c)def moves(n, a=0, b=1, c=2): stack = [(True, n, a, b, c)] while stack: tag, n, a, b, c = st... 阅读全文
posted @ 2014-02-23 13:59 LisPythoniC 阅读(228) 评论(0) 推荐(0) 编辑
摘要:我发现从人的角度来看,以最少的代码解决最复杂的问题的思维方式应该是递归,无论是以前接触到的经典的斐波拉契函数还是最近研究的Hanoi变体-4柱最优步骤生成函数(注意,不仅仅是得出最小的步骤总数).非线性递归---尾递归---迭代遗憾的是,从右到左,对计算机是越来越不友好. 而从非线性递归转化为尾递归相对来说要容易一些, 如果有一个装饰器,它能够使所有尾递归函数自动变为等价的迭代函数,那么就相当于极大扩展了递归在Python的应用空间.还真就有这种装饰器!今天无意发现的.class TailRecursive(object): """ tail_recursive 阅读全文
posted @ 2014-02-22 23:29 LisPythoniC 阅读(845) 评论(0) 推荐(0) 编辑
摘要:import re,osdef org(path=os.getcwd(),fs=None,preview=True): fs = fs or [] for root,dirs,files in os.walk(path): for f in files: if f[-4:]=='html': fp=os.path.join(root,f) s=open(fp,encoding='u8').read() for func in fs: ... 阅读全文
posted @ 2014-02-20 16:09 LisPythoniC 阅读(259) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示