• python简单算法整理


    冒泡排序

    def func(alist):
        for x in range(1,len(alist)):
            for i in range(0,len(alist)-x):
                if alist[i] > alist[i+1]:
                    alist[i], alist[i+1] = alist[i+1], alist[i]
        return alist
    print(func([1,4,2,3,6,7,8,9,0,5]))
    

    选择排序

    def func(alist):
        for x in range(0,len(alist)):
            min_num = alist[x]
            for i in range(x+1,len(alist)):
                if alist[i] < min_num:
                    alist[i], min_num = min_num, alist[i]
            alist[x] = min_num
        return alist
    print(func([1,4,2,3,6,7,8,9,0,5]))
    

    斐波那契

    def func(n):
        a = 0
        b = 1
        alist = []
        if n <= 2:
            alist.append(a)
            alist.append(b)
            return alist
        else:
            for i in range(n):
                alist.append(a)
                a, b = b, a + b
            return alist
    
    print(func(9))
    

    二分法

    def func(alist, item):
        low = 0
        high = len(alist)-1
        n = 0
        while low <= high:
            mid = int((low + high)/2)
            n += 1
            if alist[mid]==item:
                return mid
            if alist[mid]<item:
                low = mid + 1
            else:
                high = (mid-1)
        return None
    
    m=[1,2,3,4,8,9,11,12,14,18,19,20,28]
    print(func(m,14))
    

    链表

    class Node:
        def __init__(self, initdata):
            self.__data = initdata
            self.__next = None
    
        def getData(self):
            return self.__data
    
        def getNext(self):
            return self.__next
    
        def setData(self, newdata):
            self.__data = newdata
    
        def setNext(self, newnext):
            self.__next = newnext
    
    class SinCycLinkedlist:
        def __init__(self):
            self.head = Node(None)
            self.head.setNext(self.head)
    
        def add(self, item):
            temp = Node(item)
            temp.setNext(self.head.getNext())
            self.head.setNext(temp)
    
        def remove(self, item):
            prev = self.head
            while prev.getNext() != self.head:
                cur = prev.getNext()
                if cur.getData() == item:
                    prev.setNext(cur.getNext())
                prev = prev.getNext()
    
        def search(self, item):
            cur = self.head.getNext()
            while cur != self.head:
                if cur.getData() == item:
                    return True
                cur = cur.getNext()
    
            return False
    
        def empty(self):
            return self.head.getNext() == self.head
    
        def size(self):
            count = 0
            cur = self.head.getNext()
            while cur != self.head:
                count += 1
                cur = cur.getNext()
    
            return count
    
    if __name__ == '__main__':
        s = SinCycLinkedlist()
        print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
    
        s.add(19)
        s.add(86)
        print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
    
        print('86 is%s in s' % ('' if s.search(86) else ' not',))
        print('4 is%s in s' % ('' if s.search(4) else ' not',))
        print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
    
        s.remove(19)
        print('s.empty() == %s, s.size() == %s' % (s.empty(), s.size()))
    

    青蛙跳台阶

    一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
    
    fib = lambda n: n if n < 2 else 2 * fib(n - 1)
    

    快排

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    def quick(list):
        if len(list) < 2:
            return list
    
        tmp = list[0]  # 临时变量 可以取随机值
        left = [x for x in list[1:] if x <= tmp]  # 左列表
        right = [x for x in list[1:] if x > tmp]  # 右列表
        return quick(left) + [tmp] + quick(right)
    
    li = [4,3,7,5,8,2]
    print quick(li)  # [2, 3, 4, 5, 7, 8]
    
    #### 对[4,3,7,5,8,2]排序
    '''
    [3, 2] + [4] + [7, 5, 8]                 # tmp = [4]
    [2] + [3] + [4] + [7, 5, 8]              # tmp = [3] 此时对[3, 2]这个列表进行排序
    [2] + [3] + [4] + [5] + [7] + [8]        # tmp = [7] 此时对[7, 5, 8]这个列表进行排序
    '''
    

  • 相关阅读:
    充满了艰难的造题之幼儿园测试【历史考试】
    【大型网站技术实践】初级篇:借助Nginx搭建反向代理服务器
    windows下nginx的安装使用及解决80端口被占用nginx不能启动的问题
    使用hMailServer搭建免费邮件服务器(亲测可用)
    $.ajax()方法详解
    详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
    $.ajax()调用.cs里的方法实现ajax操作示例
    Asp.Net URL重写的具体实现(通过URLRewriter.dll)
    数据图
    C#解析JSON字符串总结
  • 原文地址:https://www.cnblogs.com/sq1995liu/p/14160747.html
Copyright © 2020-2023  润新知