Skip to content

Commit 9c1d8ee

Browse files
committed
update for some basic functions and common algorithms: dfs,bfs.
Signed-off-by: Jackey <smjjackey@sina.com>
1 parent ee61a09 commit 9c1d8ee

File tree

5 files changed

+84
-11
lines changed

5 files changed

+84
-11
lines changed

generator_fibonacci.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python3
2+
import sys
3+
def fibonacci(n): # 生成器函数 - 斐波那契
4+
"""
5+
在调用生成器运行的过程中,每次遇到 yield 时函数会暂停并保存当前所有的运行信息,
6+
返回 yield 的值, 并在下一次执行 next() 方法时从当前位置继续运行。
7+
:param n: the number of series
8+
:return:
9+
"""
10+
a, b, counter = 0, 1, 0
11+
while True:
12+
if (counter > n): return
13+
yield a
14+
a, b = b, a + b # a=b, b=a+b
15+
counter += 1
16+
17+
f = fibonacci(10) # f 是一个迭代器,由生成器返回生成
18+
while True:
19+
try:
20+
print(next(f), end=" ")
21+
except StopIteration:
22+
sys.exit()

iterator_filter_prime_numbers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def _not_divisible(n):
2727

2828
def primes():
2929
yield 2
30-
it = _odd_iter()
30+
it = _odd_iter() #### get a generator
3131
# print(it) # one element: 2 and a generator object
3232
while True:
33-
n = next(it)
33+
n = next(it) #### keep the whole generator: it
3434
yield n
3535
it = filter(_not_divisible(n), it)
3636

list_clear.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
1-
lst1 = [1, 2, 3]
2-
lst2 = lst1
3-
del lst1[:]
4-
print(lst2) #[]
5-
del lst1
6-
# print(lst1) # error: name 'lst1' is not defined
7-
print(lst2) #[]
8-
del lst2
9-
print(lst2) # error: name 'lst2' is not defined
1+
if __name__=="__main__":
2+
lst1 = [1, 2, 3]
3+
lst2 = lst1 ##### pass by reference
4+
lst3 = lst1.copy()
5+
# del lst1[:]
6+
lst1.clear() ###### the same effect as del lst1[:]
7+
print("lst1",lst1) #[]
8+
print("lst2",lst2) ######[] pass by reference
9+
print("lst3",lst3) #[1,2,3]
10+
del lst1
11+
try:
12+
print(lst1) # error: name 'lst1' is not defined
13+
except:
14+
print("error")
15+
print("lst2",lst2) ##############[]
16+
print("lst3",lst3) #[1,2,3]
17+
del lst2
18+
try:
19+
print(lst2) # error: name 'lst2' is not defined
20+
except:
21+
print("error")
22+
del lst3
23+
try:
24+
print(lst3) # error: name 'lst3' is not defined
25+
except:
26+
print("error")
27+
28+
l1=[1,2,3]
29+
l2=[1,2,3]
30+
l3=[3,2,1]
31+
print(l1==l2)
32+
print(l1==l3)

list_set_try.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@
3737
print("a和b中不同的元素")
3838
f = set(a).symmetric_difference(set(b)) # a和b中不同的元素
3939
print(f)
40+
print()
41+
42+
# 定义一个集合
43+
set1 = {1, 2, 3, 4, 5}
44+
# 或者使用 set 函数
45+
list1 = [6, 7, 7, 8, 8, 9]
46+
set2 = set(list1)
47+
set2.add(10) # 添加新元素
48+
print("set2",set2) # set([6,7,8,9,10]) 去掉重复内容,
49+
set2.update([10,11,12])
50+
print("set2", set2)
51+
try:
52+
set3 = frozenset(list1) # 固定集合
53+
set3.add(10) # 固定集合不能添加元素
54+
print("set3",set3)
55+
except:
56+
print("error")
4057

4158

4259

print.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
if __name__ == "__main__":
3+
PI = 3.141592653
4+
print('%10.3f'%PI)
5+
6+
print("PI=%.*f" % (3, PI))
7+
# 用*从后面的元组中读取字段宽度或精度,可以读取出来精度是3位
8+
# PI=3.142
9+
# 没有指定宽度,所以不需要缩进
10+
print("PI=%*.3f" % (10, PI)) # 精度为3,总长为10.
11+

0 commit comments

Comments
 (0)