【Python】DS的基础学习笔记14:再谈编程

第十四章 再谈编程

14.1 Python之禅

import this

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
    如果方案很难解释,很可能不是一个好的方案,反之亦然

【个人感悟】

  1. 首先要行动起来,编写行之有效的代码
  2. 如果都能解决问题,选择更加简单的方案
  3. 整齐、易读、可维护性、可拓展性好
  4. 强壮、健壮、容错性好
  5. 响应速度快、占用空间少

有些时候,鱼和熊掌不可兼得,根据实际情况进行相应取舍

14.2 时间复杂度分析

14.2.1 代数分析

求最大值和排序

import numpy as np
x = np.random.randint(100, size=10)
print(x)

[51 92 14 71 60 20 82 86 74 74]

  • 寻找最大值的时间复杂度为 O ( n ) O(n) O(n)
  • 选择排序时间复杂度为 O ( n 2 ) O(n^2) O(n2)

代数分析

def one(x):
    """常数函数"""
    return np.ones(len(x))


def log(x):
    """对数函数"""
    return np.log(x)


def equal(x):
    """线性函数"""
    return x


def n_logn(x):
    """nlogn函数"""
    return x*np.log(x)


def square(x):
    """平方函数"""
    return x**2


def exponent(x):
    """指数函数"""
    return 2**x
import matplotlib.pyplot as plt
plt.style.use("seaborn-whitegrid")

t = np.linspace(1, 20, 100)
methods = [one, log, equal, n_logn, square, exponent]
method_labels = ["$y = 1$", "$y = log(x)$", "$y = x$", "$y = xlog(x)$", "$y = x^2$", "$y = 2^x$"]
plt.figure(figsize=(12, 6))
for method, method_label in zip(methods, method_labels):
    plt.plot(t, method(t), label=method_label, lw=3)
plt.xlim(1, 20)
plt.ylim(0, 40)
plt.legend()
plt.show()

运行结果
我们的最爱:常数函数和对数函数
勉强接受:线性函数和nlogn函数
难以承受:平方函数和指数函数

14.2.2 三集不相交问题

问题描述:假设有A、B、C三个序列,任意序列内部没有重复元素,欲知晓三个序列交集是否为空

import random
import time


def create_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 = create_sequence(100)
def no_intersection_1(A, B, C):
    for a in A:
        for b in B:
            for c in C:
                if a == b == c:
                    return False
    return True


start = time.perf_counter()
no_intersection_1(A, B, C)
end = time.perf_counter()
print("用时{:.5f}秒".format(end-start))

用时0.02033秒

def no_intersection_2(A, B, C):
    for a in A:
        for b in B:
            if a == b:
                for c in C:
                    if a == c:
                        return False
    return True


start = time.perf_counter()
no_intersection_2(A, B, C)
end = time.perf_counter()
print("用时{:.5f}秒".format(end-start))

用时0.00014秒

res_n_3 = []
res_n_2 = []

for n in [10, 20, 100]:
    A, B, C = create_sequence(n)
    start_1 = time.time()
    for i in range(100):
        no_intersection_1(A, B, C)
    end_1 = time.time()
    for i in range(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_intersection_1", *res_n_3))
print("{0:<25}{1:<15}{2:<15}{3:<15}".format("no_intersection_2", *res_n_2))

运行结果

14.2.3 元素唯一性问题

问题描述:A中的元素是否唯一

def unique_1(A):
    for i in range(len(A)):
        for j in range(i+1, len(A)):
            if A[i] == A[j]:
                return False
    return True
def unique_2(A):
    A_sort = sorted(A)  # O(nlogn)
    for i in range(len(A_sort)-1):
        if A[i] == A[i+1]:
        return False
    return True
import random
import time

res_n_2 = []
res_n_log_n = []

for n in [100, 1000]:
    A = list(range(n))
    random.shuffle(A)
    start_1 = time.time()
    for i in range(100):
        unique_1(A)
    end_1 = time.time()
    for i in range(100):
        unique_2(A)
    end_2 = time.time()
    res_n_2.append(str(round((end_1-start_1)*1000))+"ms")
    res_n_log_n.append(str(round((end_2-end_1)*1000))+"ms")

print("{0:<13}{1:<15}{2:<15}".format("方法", "n=100", "n=1000"))
print("{0:<15}{1:<15}{2:<15}".format("unique_1", *res_n_2))
print("{0:<15}{1:<15}{2:<15}".format("unique_2", *res_n_log_n))

运行结果

14.2.4 第n个斐波那契数列

a n + 2 = a n + 1 + a n a_{n+2}=a_{n+1}+a_{n} an+2=an+1+an

def bad_fibonacci(n):
    if n <= 1:
        return n
    else:
        return bad_fibonacci(n-2) + bad_fibonacci(n-1)

O ( 2 n ) O(2^n) O(2n)

def good_fibonacci(n):
    i, a, b = 0, 0, 1
    while i < n:
        a, b = b, a+b
        i += 1
    return a

O ( n ) O(n) O(n)

start_1 = time.perf_counter()
bad_fibonacci(10)
end_1 = time.perf_counter()

start_2 = time.perf_counter()
good_fibonacci(10)
end_2 = time.perf_counter()
print("运行用时{:.5f}".format((end_1 - start_1)*1000)+"ms")
print("运行用时{:.5f}".format((end_2 - end_1)*1000)+"ms")

运行用时0.01330ms
运行用时0.00140ms

14.2.5 最大盛水容器(leetcode第11题)

问题描述
暴力求解——双循环

def max_area_double_cycle(height):
    """"暴力穷举双循环"""
    i_left, i_right, max_area = 0, 0, 0
    for i in range(len(height)-1):
        for j in range(i+1, len(height)):
            area = (j-i) * min(height[j], height[i])
            if area > max_area:
                i_left, i_right, max_area = i, j, area
    return i_left, i_right, max_area

O ( n 2 ) O(n^2) O(n2)

height = np.random.randint(1, 50, size=10)
print(height)
print(max_area_double_cycle(height))

[39 29 15 43 8 21 39 19 23 11]
(0, 6, 234)

plt.bar(range(10), height, width=0.5)
plt.xticks(range(0, 10, 1))
plt.show()

运行结果

双向指针

def max_area_bothway_points(height):
    """双向指针"""
    i = 0
    j = len(height) - 1
    i_left, i_right, max_area = 0, 0, 0
    while i < j:
        area = (j-i)*min(height[i], height[j])
        if area > max_area:
            i_left, i_right, max_area = i, j, area
        if height[i] == min(height[i], height[j]):
            i += 1
        else:
            j -= 1
    return i_left, i_right, max_area

O ( n ) O(n) O(n)

print(max_area_bothway_points(height))

(0, 6, 234)
结果与暴力求解法一致,但时间复杂度大大降低

我们将两种方法进行比较

double_cycle = []
bothway_points = []

for n in [5, 50, 500]:
    height = np.random.randint(1, 50, size=n)
    start_1 = time.time()
    for i in range(100):
        max_area_double_cycle(height)
    end_1 = time.time()
    for i in range(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))

运行结果

14.2.6 是不是时间复杂度低就一定好?

并不一定
当n在某一范围之内,举一极端的例子
100000 n V S 0.00001 n 2 100000n\quad VS\quad 0.00001n^2 100000nVS0.00001n2
但当n无穷大时, n 2 n^2 n2收敛速度肯定较慢

14.2.7 影响运算速度的因素

  • 硬件
  • 软件
  • 算法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值