The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Beautiful is better than ugly 整齐、易读胜过混乱、晦涩
Simple is better than complex 简约胜过复杂
Complex is better than complicated 复杂胜过晦涩
Flat is better than nested 扁平胜过嵌套
Now is better than never. Although never is often better than right now. 理解一:先行动起来,编写行之有效的代码,不要企图一开始就编写完美无缺的代码 理解二:做比不做要好,但是盲目的不加思考的去做还不如不做
If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. 如果方案很难解释,很可能不是有一个好的方案,反之亦然
import random
defcreat_sequence(n):
A = random.sample(range(1,1000), k=n)
B = random.sample(range(1000,2000), k=n)
C = random.sample(range(2000,3000), k=n)return A, B, C
A, B, C = creat_sequence(100)defno_intersection_1(A, B, C):for a in A:for b in B:for c in C:if a == b == c:returnFalsereturnTrue
%timeit no_intersection_1(A, B, C)
36.7 ms ± 2.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
时间复杂度O(n3)
defno_intersection_2(A, B, C):for a in A:# a有n个for b in B:if a == b:# 每个a最多与一个b相等,与n-1个b不等for c in C:# 1个相等执行n次if a == c:returnFalsereturnTrue
%timeit no_intersection_2(A, B, C)
301 µs ± 37.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
时间复杂度O(n2)
import time
res_n_3 =[]
res_n_2 =[]
for n in[10,20,100]:
A, B, C = creat_sequence(n)
start_1 = time.time()for i inrange(100):
no_intersection_1(A, B, C)
end_1 = time.time()for i inrange(100):
no_intersection_2(A, B, C)
end_2 = time.time()
res_n_3.append(str(round((end_1 - start_1)*1000))+"ms")
res_n_2.append(str(round((end_2 - end_1)*1000))+"ms")
print("{0:<23}{1:<15}{2:<15}{3:<15}".format("方法","n=10","n=20","n=100"))print("{0:<25}{1:<15}{2:<15}{3:<15}".format("no_inte rsection_1",*res_n_3))# *代表解包print("{0:<25}{1:<15}{2:<15}{3:<15}".format("no_intersection_2",*res_n_2))
defmax_area_bothway_points(height):"""双向指针法"""
i =0
j =len(height)-1
i_left, j_right, max_area=0,0,0while i < j:
area =(j-i)*min(height[i], height[j])if area > max_area:
i_left, j_right, max_area = i, j, area
if height[i]==min(height[i], height[j]):
i +=1else:
j -=1return i_left, j_right, max_area
时间复杂度O(n)
double_cycle =[]
bothway_points =[]
for n in[5,50,500]:
height = np.random.randint(1,50, size=n)
start_1 = time.time()for i inrange(100):
max_area_double_cycle(height)
end_1 = time.time()for i inrange(100):
max_area_bothway_points(height)
end_2 = time.time()
double_cycle.append(str(round((end_1 - start_1)*1000))+"ms")
bothway_points.append(str(round((end_2 - end_1)*1000))+"ms")
print("{0:<15}{1:<15}{2:<15}{3:<15}".format("方法","n=5","n=50","n=500"))print("{0:<13}{1:<15}{2:<15}{3:<15}".format("暴力循环",*double_cycle))print("{0:<13}{1:<15}{2:<15}{3:<15}".format("双向指针",*bothway_points))