python 演算法學習部分代碼記錄篇章1

來源:互聯網
上載者:User

標籤:coding   判斷   記錄   shell   bre   add   obj   cas   pytho   

# -*- coding: utf-8 -*-# @Date    : 2017-08-19 20:19:56# @Author  : lileilei‘‘‘那麼演算法和資料結構是什麼呢,答曰兵法‘‘‘‘‘‘a+b+c=1000 and a*a+b*b=c*c 求a,b,c‘‘‘# import time# start_time=time.time()# for a in range(1000):#使用枚舉法#     for b in range(1000):#         for c in range(1000):#             if a+b+c==1000 and a*a+b*b==c*c:#                 print(a,b,c)# print(time.time()-start_time)# import time #方法2# start_time=time.time()# for a in range(1000):#     for b in range(1000):#         c=1000-a-b#         if a+b+c==1000 and a*a+b*b==c*c:#             print(a,b,c)# print(time.time()-start_time)class Stack(object):    """棧"""    def __init__(self):         self.__items = []    def is_empty(self):        """判斷是否為空白"""        return self.__items == []    def push(self, item):        """加入元素"""        self.__items.append(item)    def pop(self):        """彈出元素"""        return self.__items.pop()    def peek(self):        """返回棧頂元素"""        return self.__items[len(self.__items)-1]    def size(self):        """返回棧的大小"""        return len(self.__items)# if __name__ == "__main__":#     stack = Stack()#     stack.push("hello")#     stack.push("world")#     stack.push("itcast")#     print (stack.size())#     print (stack.peek())#     print (stack.pop())#     print (stack.pop())#     print (stack.pop())class Queue(object):    ‘‘‘隊列‘‘‘    def __init__(self):        self.__list=[]    def addqueue(slef,item):        #self.__list.append(item)        self.__list.insert(0,item)    def dequeue(self):        return self.__list.pop()    def is_empty(self):        return self.__list==[]    def size(self):        return len(self.__list)class Dqueue(object):    ‘‘‘雙端隊‘‘‘    def __init__(self):        self.__list=[]    def add_front(slef,item):        self.__list.insert(0,item)    def add_re(self,item):        self.__list.insert(item)    def dequeue(self):        return self.__list.pop()    def requeue(self):        return self.__list.pop(0)    def is_empty(self):        return self.__list==[]    def size(self):        return len(self.__list)def buule_sor(alist):#冒泡    n=len(alist)    for i in range(n-1):        for j in range(n-1-i):            count=0            if alist[j]>alist[j+1]:                alist[j],alist[j+1]=alist[j+1],alist[j]                count+=1        if 0==count:            returndef select_sort(alist):#選擇排序    n=len(alist)    for j in range(n-1):        min=j        for i in range(j+1,n):            if alist[min] > alist[i]:                min=i        alist[j],alist[min]=alist[min],alist[j]def insert_sort(alist):‘‘‘插入排序‘‘‘    n=len(alist)    for j in range(1,n):        i=j        while  i>0:            if alist[i]<alist[i-1]:                alist[i],alist[i-1]=alist[i-1],alist[i]            i-=1def shell_sort(alist):‘‘‘希爾排序‘‘‘    n=len(alist)    gap=n//2    while gap>0:        for j in range(gap,n):            i=j            while i>0:                if alist[i]<alist[i-gap]:                    alist[i],alist[i-gap]=alist[i-gap],alist[i]                    i-=gap                else:                    break        gap//=2def quick_sort(alist,first,last):‘‘‘快速排序‘‘‘    if first>=last:        return    mid_value=alist[first]    low=first    high=last    while low<high:        while low <high and alist[high]>=mid_value:            high-=1        alist[low]=alist[high]        while low <high and alist[low]<mid_value:            low+=1        alist[high]=alist[low]    alist[low]=mid_value    quick_sort(alist,first,low-1)    quick_sort(alist,low+1,last)def me_sort(alist):‘‘‘歸併排序‘‘‘    n=len(alist)    if n<=1:        return alist    mid=n//2    left=me_sort(alist[:mid])    right=me_sort(alist[mid:])    left_point,right_porint=0,0    result=[]    while  left_point<len(left) and right_porint<len(right):        if left[left_point] <right[right_porint]:            result.append(left[left_point])            left_point+=1        else:            result.append(right[right_porint])            right_porint+=1    result+=left[left_point:]    result+=right[right_porint:]    return resultdef binary_search(alist,item):#二分尋找 遞迴    n=len(alist)    if n>0:        mid=n//2        if alist[mid]==item:            return True        elif item<alist[mid]:            return binary_search(alist[:mid],item)        else:            return binary_search(alist[mid+1:],item)    return Falsedef brin_serce2(alist,item):#二分非遞迴    n=len(alist)    first=0    lasr=n-1    while first <=lasr:        mid = (first + lasr) // 2        if alist[mid]==item:            return  True        elif item<alist[mid]:            lasr=mid-1        else:            first=mid+1    return Falseif __name__ == ‘__main__‘:    listy=[54,67,76,23,34]    print(brin_serce2(listy,55))

 

python 演算法學習部分代碼記錄篇章1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.