From b3cabb93d15ee03ea476ffb37ea210274b35f6b7 Mon Sep 17 00:00:00 2001 From: SJ Date: Tue, 12 Apr 2016 18:15:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 205.md | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/205.md b/205.md index e82dc74..2ae0e2a 100644 --- a/205.md +++ b/205.md @@ -202,27 +202,28 @@ #网友frankwang分享一段关于素数的代码,供各位参考: -def find_primes(n): - primesList = [] - for x in range(2, n+1): - isPrime = True - for y in range(2, int(x**0.5) + 1): #x**0.5 相当于math.sqrt(x) - if x % y == 0: - isPrime = False - break - if isPrime: - primesList.append(x) - - print(primesList) -if __name__ == "__main__": - max = int(input('Find primes up to: ')) - find_primes(max) + def find_primes(n): + primesList = [] + for x in range(2, n+1): + isPrime = True + for y in range(2, int(x**0.5) + 1): #x**0.5 相当于math.sqrt(x) + if x % y == 0: + isPrime = False + break + if isPrime: + primesList.append(x) + print(primesList) + + if __name__ == "__main__": + max = int(input('Find primes up to: ')) + find_primes(max) 代码保存后运行: # 打印结果如下: -Find primes up to: 100 -[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] + Find primes up to: 100 + [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] + ##编写函数的注意事项 From fb95288bcf715a3ff2272c9cbcd2e11e8125ce16 Mon Sep 17 00:00:00 2001 From: SJ Date: Thu, 28 Apr 2016 14:52:13 +0800 Subject: [PATCH 2/2] Update 214.md/235.md 214.md: Add python3 comment. 235.md: Format url ; Fix fractal-tree example error. --- 214.md | 2 +- 235.md | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/214.md b/214.md index 765108b..d8e0877 100644 --- a/214.md +++ b/214.md @@ -109,7 +109,7 @@ def __iter__(self): return self - def next(self): + def next(self): #python3: def __next__(self): fib = self.a if fib > self.max: raise StopIteration diff --git a/235.md b/235.md index 855157e..6df970a 100644 --- a/235.md +++ b/235.md @@ -251,7 +251,7 @@ nested的汉语意思是“嵌套的,内装的”,从字面上读者也可 contextlib.contextmanager是一个装饰器,它作用于生成器函数(也就是带有yield的函数),一单生成器函数被装饰以后,就返回一个上下文管理器,即contextlib.contextmanager因为装饰了一个生成器函数而产生了`__enter__()`和`__exit__()`方法。例如: -特别要提醒,被装饰的生成器函数只能产生一个值,否则就会抛出RuntimeError异常;如果有as子句,则所产生的值,会通过as子句赋给某个变量,就如同前面那样,例如下面的示例(本示例来自:http://www.ibm.com/developerworks/cn/opensource/os-cn-pythonwith/index.html)。 +特别要提醒,被装饰的生成器函数只能产生一个值,否则就会抛出RuntimeError异常;如果有as子句,则所产生的值,会通过as子句赋给某个变量,就如同前面那样,例如下面的示例(本示例来自:http://www.ibm.com/developerworks/cn/opensource/os-cn-pythonwith/index.html )。 #!/usr/bin/env python # coding=utf-8 @@ -274,7 +274,7 @@ contextlib.contextmanager是一个装饰器,它作用于生成器函数(也 the word is: contextmanager demo. after yield. -为了好玩,再借用网上的一个示例,理解这个装饰器的作用(下面代码来自:http://preshing.com/20110920/the-python-with-statement-by-example/),代码中用到了`cairo`模块,该模块的安装方法是: +为了好玩,再借用网上的一个示例,理解这个装饰器的作用(下面代码来自:http://preshing.com/20110920/the-python-with-statement-by-example/ ),代码中用到了`cairo`模块,该模块的安装方法是: sudo apt-get install libcairo2-dev @@ -302,7 +302,7 @@ contextlib.contextmanager是一个装饰器,它作用于生成器函数(也 cr.line_to(0, 0) cr.stroke() cr.scale(0.72, 0.72) - if angle > 0.72: + if angle > 0.12: for a in [-angle, angle]: with saved(cr): cr.rotate(a)