上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 23 下一页
摘要: 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 阅读(267) 评论(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 阅读(292) 评论(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 阅读(444) 评论(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 阅读(840) 评论(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 阅读(257) 评论(0) 推荐(0) 编辑
摘要: 今天,我总算搞清楚“回车”(carriage return)和“换行”(line feed)这两个概念的来历和区别了。在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33)的玩意,每秒钟可以打10个字符。但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两个字符。要是在这0.2秒里面,又有新的字符传过来,那么这个字符将丢失。于是,研制人员想了个办法解决这个问题,就是在每行后面加两个表示结束的字符。一个叫做“回车”,告诉打字机把打印头定位在左边界;另一个叫做“换行”,告诉打字机把纸向下移一行。这就是“换行”和“回车”的来历,从它们的英语名字上也可以看 阅读全文
posted @ 2014-01-31 11:33 LisPythoniC 阅读(349) 评论(0) 推荐(0) 编辑
摘要: >>> import datetime>>> class c(): @property def noww(self): return datetime.datetime.now() >>> c.noww>>> x=c()>>> x.nowwdatetime.datetime(2014, 1, 27, 13, 17, 14, 725610)>>> x.nowwdatetime.datetime(2014, 1, 27, 13, 17, 27, 8740)>>> x.now 阅读全文
posted @ 2014-01-27 13:19 LisPythoniC 阅读(240) 评论(0) 推荐(0) 编辑
摘要: >>> def f(): a=1 return [i+a for i in range(3)]>>> f()[1, 2, 3]>>> def f(): a=1 return [i+eval('a') for i in range(3)]>>> f()Traceback (most recent call last): File "", line 1, in f() File "", line 3, in f return [i+eval('a') fo 阅读全文
posted @ 2014-01-26 17:57 LisPythoniC 阅读(227) 评论(0) 推荐(0) 编辑
上一页 1 ··· 12 13 14 15 16 17 18 19 20 ··· 23 下一页