#!/usr/bin/env python
# encoding: utf-8
"""
@author: HuRuiFeng
@file: int_push_back.py
@time: 2019/4/9 22:34
@desc: 第32讲:连续放入
       采用向量数据结构,持续放入n个整数
"""
import time

from common_lib.vector import Vector

if __name__ == '__main__':
    n = 1000000
    start = time.process_time()
    V = Vector()
    for i in range(n):
        V.push_back(0)
    end = time.process_time()
    print("运行时间(s):", end - start)
@time: 2019/4/9 22:41
@desc: 第32讲:连续放入
       采用向量数据结构,持续放入n个节点
       由于python中没有指针的概念,故与结构体的push back是一样的
"""
import time

from common_lib.vector import Vector

global m

class xnode(object):
    def __init__(self, d):
        data = []
        for i in range(m):
            data.append(d)
        data.append(0)

if __name__ == '__main__':
    n = 1000000
    m = 16
    V = Vector()
    start = time.process_time()
    for i in range(n):
        p = xnode(i)
        V.push_back(p)
    end = time.process_time()
    print("运行时间(s):", end - start)
    for v in V:
        del v
Exemple #3
0
#!/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())
Exemple #4
0
    # 定义一个初始长度为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的末尾元素
        print(B.back())
        # 删除向量V的末尾元素
        B.pop_back()

    # 接收未知长度的自然数输入序列, 以负数作为输入终止
    C = Vector()
    # 基于push_back操作的速度非常快.
    while True:
        num = int(input())
        if num >= 0:
Exemple #5
0
@time: 2019/4/9 22:52
@desc: 第32讲:连续放入
       采用向量数据结构,持续放入n个节点
"""

import time

from common_lib.vector import Vector

global m


class xnode(object):
    def __init__(self, d):
        data = []
        for i in range(m):
            data.append(d)
        data.append(0)


if __name__ == '__main__':
    n = 1000000
    m = 16
    V = Vector()
    start = time.process_time()
    for i in range(n):
        V.push_back(xnode(i))
    end = time.process_time()
    print("运行时间(s):", end - start)
    for v in V:
        del v
Exemple #6
0
@file: description.py
@time: 2019/2/9 20:57
@desc: 第15讲:以集合描述算法-学会用集合的语言实现算法
"""

from common_lib.set import Set
from common_lib.vector import Vector

if __name__ == '__main__':
    # 差集
    A = Set([3, 2, 1, 4, 7, 9, 11])
    B = Set([6, 2, 9])
    C = Vector()
    for i, a in enumerate(A):
        if a not in B:
            C.push_back(a)
    print(C)

    # 转存
    D = Set([3, 5, 1, 7, 2, 8, 0])
    E = Set()
    while not D.empty():
        if D.begin() % 2 == 0:
            E.insert(D.begin())
        D.erase(D.begin())

    print(E)

    # 动态变化
    F = Set(["English", "Ability", "Algorithm", "Faith"])
    while F.size() > 1:
if __name__ == '__main__':

    n = 42
    c = 1

    # 定义
    V1 = Vector(n)

    # 初始化
    A1 = [c for i in range(n)]
    V2 = Vector(n, c)

    # 列表初始化
    V3 = Vector([1, 2, 3, 4, 5])
    print(V3)

    # 动态变化
    V3.push_back(6)
    print(len(V3))
    print(V3.front(), V3.back())
    V3.pop_back()
    print(len(V3))
    print(V3.front(), V3.back())

    # 迭代器
    for _, v in enumerate(V3):
        print(v, end=" ")
    print()