#!/usr/bin/env python # encoding: utf-8 """ @author: HuRuiFeng @file: doubling.py @time: 2019/2/13 21:55 @desc: 第25讲:容量之妙-向量容量是高效的来源 """ from common_lib.vector import Vector if __name__ == '__main__': n = 40 #size()返回当前元素个数, capacity()返回当前容量 A = Vector() for i in range(n): print(A.size(), A.capacity()) A.push_back(0) print(A.size(), A.capacity()) B = Vector() # 提前预留容量n. B.resize(n) for i in range(n): print(B.size(), B.capacity()) B.push_back(0) print(B.size(), B.capacity())
@time: 2019/2/9 23:30 @desc: 第20讲:常见运行时间-给出代码并分析常见运行时间 """ from common_lib.vector import Vector import time import bisect if __name__ == '__main__': N = 10000000 v = Vector(N) # 线性时间 start = time.process_time() length = v.size() for i, item in enumerate(v): v[i] = length - i end = time.process_time() print("运行时间(s):", end - start) # 线对时间 start = time.process_time() v.sort() end = time.process_time() print("运行时间(s):", end - start) # 对数时间 start = time.process_time() bisect.bisect(v, 1) end = time.process_time()
""" @author: HuRuiFeng @file: vector_example.py @time: 2019/2/13 21:09 @desc: 第24讲-向量用法 """ from common_lib.vector import Vector if __name__ == '__main__': # 定义一个初始长度为10的向量A, 可用位置数为10. A = Vector(10) # 下标i从0到A.size()-1对A中每个元素赋值i. # 由于python的list可以放置任何类型元素 # 向量的下标用法与数组相同, 但要注意应保证向量中实有元素个数就是size(). for i in range(A.size()): A[i] = i for i in range(A.size()): A[i] = 1 # 定义一个长度为5的向量B, 初始元素全为3. B = Vector(5, 3) # 在向量B末尾加入100个2. B.resize(B.size() + 100, 2) # 在向量V的尾部加入4 B.push_back(4) # 迭代器的另一种用法,使用列表推导 B = Vector([value * 2 for i, value in enumerate(B)]) # 若向量B不为空,则持续输出其尾部元素并删除之 while not B.empty(): # 输出向量V的末尾元素
#!/usr/bin/env python # encoding: utf-8 """ @author: HuRuiFeng @file: order_statistics_set.py @time: 2019/2/9 19:03 @desc: 第13讲:集合中的次序统计量-寻找集合中第k小的元素 """ from common_lib.set import Set from common_lib.vector import Vector if __name__ == '__main__': S = Set([3, 2, 1, 4, 5]) k = int(input("请输入k: ")) if k<=0 or k > S.size(): print("超出范围!") else: # 第一种方法 print(sorted(S.copy())[k-1]) # 第二种方法 V = Vector(k-1) for i in range(0, k-1): V[i] = S.begin() S.discard(S.begin()) print(S.begin()) for i in range(V.size()): S.insert(V[i]) print(S)