算法

# Stack() 创建一个新的空栈    创建空列表
class Stack():
    def __init__(self):
        self.alist = []

    # push(item) 添加一个新的元素item到栈顶  appeng
    def push(self, item):
        self.alist.append(item)

    # pop() 弹出栈顶元素
    def pop(self):
        self.alist.pop(-1)

    # peek() 返回栈顶元素
    def peek(self):
        return self.alist[-1]

    # is_empty() 判断栈是否为空
    def is_empty(self):
        if len(self.alist) == 0:
            print('空栈')
        else:
            print('不为空')

    # size() 返回栈的元素个数
    def size(self):
        return len(self.alist)


stark = Stack()
stark.push(1)
stark.push(2)
stark.push(3)
stark.push(4)
print(stark.size())
stark.is_empty()
stark.pop()
print(stark.size())
print(stark.peek())

队列

class Queue():
    def __init__(self):
        self.alist = []

    def enqueue(self, item):
        self.alist.append(item)

    # 创建一个空的队列
    # alist = []
    # enqueue(item)
    # 往队列中添加一个item元素
    # alist.append()
    # dequeue()
    # 从队列头部删除一个元素
    def dequeue(self):
        self.alist.pop(0)

    # alist.pop(0)
    # is_empty()
    # 判断一个队列是否为空
    # size()
    # 返回队列的大小
    def is_empty(self):
        if len(self.alist) == 0:
            print('空栈')

    def size(self):
        return len(self.alist)


queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.enqueue(4)
print(queue.size())
queue.dequeue()
print(queue.size())
queue.is_empty()

冒泡排序(重点)

def maopao(alist):
    for i in range(len(alist) - 1):
        for j in range(len(alist) - 1 - i):
            if alist[j] > alist[j + 1]:
                alist[j], alist[j + 1] = alist[j + 1], alist[j]
    print(alist)


maopao([3, 5, 2, 8, 1, 9, 4, 7])

选择排序(重点)

def Maopao(alist):
    for i in range(len(alist) - 1):
        min = i
        for j in range(i + 1, len(alist)):
            if alist[min] > alist[j]:
                min = j
        if min != i:
            alist[min], alist[i] = alist[i], alist[min]
    print(alist)


Maopao([3, 5, 2, 8, 1, 9, 4, 7])

二分法查找 有序列表查找

def func(list1, item):
    first = 0
    end = len(list1) - 1
    while first <= end:
        mid = (first + end) // 2
        if item == list1[mid]:
            return True
        elif item > list1[mid]:
            first = mid + 1
        else:
            end = mid - 1
    return False


print(func([i for i in range(1, 101)], 1))

斐波那契数列

class FeiBo():
    def __init__(self, n):
        self.n = n  # 斐波那契的个数
        self.num1 = 0  # 斐波那契第一个数
        self.num2 = 1  # 斐波那契第二个数
        self.index = 0  # 下标

    def __iter__(self):
        return self

    def __next__(self):
        if self.index < self.n:  # 下标小于个数
            num = self.num1  # 用来存放斐波那契数
            self.num1, self.num2 = self.num2, self.num1 + self.num2
            self.index += 1
            return num
        else:
            raise StopIteration  # 抛出异常


feibo = FeiBo(8)
print(list(feibo))

(递归)斐波那契

def Feibo(n):
    a, b, index = 1, 1, 0
    while index < n:
        num = a
        a, b = b, a + b
        index += 1
        yield num


f = Feibo(5)
print(list(f))

用生成器实现n以内的素数

def fun(n):
    for i in range(1, n):
        list2 = []
        for j in range(1, i + 1):
            if i % j == 0:
                list2.append(j)
        if len(list2) == 2:
            yield i


print(list(fun(n=int(input('请输入个数:')))))

单链表的增删改查

#定义一个结点
class LinkNode(object):
    def __init__(self, val):
        self.val = val
        self._next = None


# 链表的相关操作
class Link(object):
    def __init__(self):
        self._head = None

    def is_empty(self):
        return self._head == None

    # 头部增加一个节点
    def add(self, val):
        node = LinkNode(val)
        node._next = self._head
        self._head = node

    # 尾部添加一个结点
    def append(self, val):
        node = LinkNode(val)
        if self._head == None:
            self._head = node
        else:
            cur = self._head
            while cur._next != None:
                cur = cur._next
            cur._next = node

    # 求出连表长度
    def length(self) -> int:
        if self._head == None:
            return 0
        cur = self._head
        len = 0
        while cur._next != None:
            len += 1
            cur = cur._next
        return len + 1

    # 指定位置添加结点

    def insert(self, pos, val):
        if pos < 0:
            return False
        if pos > self.length():
            return False
        cur = self._head
        pre = None
        i = 0  # 位置
        node = LinkNode(val)
        while cur != None:
            if i == pos:
                pre._next = node
                node._next = cur
                return True
            i += 1
            pre = cur
            cur = cur._next

    # 删除所有指定值得结点
    def del_val(self, val):
        cur = self._head
        pre = None  # cur的前驱节点
        while cur != None:
            if cur.val == val:
                if pre == None:
                    self._head = None
                else:
                    pre._next = cur._next
                    cur = pre._next
            else:
                pre = cur
                cur = cur._next

    def printf(self):
        cur = self._head
        while cur != None:
            print("{}".format(cur.val))
            cur = cur._next
        # print("{}".format(cur.val)) #尾节点不在循环里,单独打印
        print("len={}\n".format(self.length()))


if __name__ == "__main__":
    link = Link()
    link.add(1)
    link.add(2)
    link.append(3)
    link.append(4)
    link.append(5)
    link.append(3)
    link.insert(3, 6)
    link.del_val(3)
    print(link.is_empty())
    link.printf()

快排


list_before = [2, 4, 9, 3, 4, 5, 2, 1, 5, 6, 4, 2, 7, 8, 6, 0]
def quickSort(list_before):
    if len(list_before) < 2:
        return list_before
    left_part = [left_ele for left_ele in list_before[1:] if left_ele < list_before[0]]
    right_part = [right_ele for right_ele in list_before[1:] if right_ele > list_before[0]]
    equal_part = [equal_ele for equal_ele in list_before[0:] if equal_ele == list_before[0]]
    return quickSort(left_part) + equal_part + quickSort(right_part)


list_after = quickSort(list_before)
print(list_after)
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页