Code implementation and improvement of algorithm diagram

Source: Internet
Author: User

Code implementation and improvement of algorithm diagram please feel free to watch the show
    • Two-point Search
    • Arrays and Linked lists
    • Recursive
    • Recursive conditions and baseline conditions
    • Quick Sort
    • Hash table
    • Breadth First Search
    • Dixtra algorithm
    • Greedy algorithm

Two-point Search
def bin_search(list,item):    low = 0    high = len(list) - 1        while low<=high:        mid = (low+high)//2 #得到中间值         guess = list[mid]        if guess==item:            return mid        elif guess>item:            high = mid-1        else:            low = mid+1        return Nonefunc = lambda x:x%2!=0my_list = list(filter(func,range(0,10)))print(my_list)print(bin_search(my_list,2))print(bin_search(my_list,5))
[1, 3, 5, 7, 9]None2

Array and list selection sorting
def findSmall(arr):#找到最小    small = arr[0]    small_index = 0    for i in range(1,len(arr)):        if arr[i]<small:            small = arr[i]            small_index = i    return (small_index,small)def selectionSelect(arr):#选择排序,升序    newArr = []    for i in range(len(arr)):        small_index = findSmall(arr)[0]        newArr.append(arr.pop(small_index))    return newArrprint(selectionSelect([i for i in range(10,0,-1)]))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Recursive box Lookup iterative notation
def lookForKey(mainBox):    pile = mainBox.makePileToLook()    while len(pile):        box = pile.grabBox()        for item in box:            if item.isBox():                pile.append(item)            elif item.isKey():                print("found the key!")
Recursive notation
def lookForKey(box):    for item in box:        if item.isBox():            lookForKey(item)        elif item.isKey():            print(‘Found the key at ‘,item)

Baseline conditions and recursion conditions
def countdown(i):    print(i)    if i-1:        countdown(i-1)    else : returncountdown(5)
54321

Quick sort divide and conquer
def Sum(arr):    if len(arr):        return arr[0] + Sum(arr[1:])    else:        return 0Sum([i for i in range(1,101)])
5050
Maximum value found
‘‘‘错误的写法,out of rangedef getMax(arr,index=0):    if len(arr)>1:        new_index = index + 1        print(new_index,len(arr))        return arr[index] if arr[index]>getMax(arr[new_index:],new_index) else getMax(arr[new_index:],new_index)    else:        return arr[index]        ‘‘‘    def getMax(arr):    if arr and len(arr)>1:        return arr[0] if arr[0] > getMax(arr[1:]) else getMax(arr[1:])    else:        return arr[0]import randomList = [i for i in range(6)]random.shuffle(List)print(List)getMax(List)
[1, 4, 5, 2, 3, 0]5
Quick Sort
def quickSort(arr):    if len(arr)<2:        return arr #基线条件,为空或者只含有一个元素的数组    else:        pivot = arr[0] # 递归条件,这里可以随机选取的        small= [i for i in arr[1:] if i<=pivot] #小于基准值组成的子数组        big  = [i for i in arr[1:] if i>pivot]        return quickSort(small) +[pivot] + quickSort(big)print(quickSort([10,5,3]))
[3, 5, 10]
Quick Sort improvements (personal code, possibly bugs)
from random import randrangedef quickSort(arr):    if len(arr)<2:        return arr    else:        flag = 0        for i in range(0,len(arr)-1):            if arr[i]>arr[i+1]:                flag = 1                break        if flag:            index = randrange(0,len(arr))            pivot = arr[index]                        small = [arr[i]  for i in range(0,len(arr)) if i!=index and arr[i]<=pivot]            big = [arr[i]  for i in range(0,len(arr)) if i!=index and arr[i]>pivot]                        return quickSort(small)+[pivot]+quickSort(big)        else:            return arrprint(quickSort([10,5,3,-5]))
[-5, 3, 5, 10]

The implementation of the hash table Python is the dictionary DNS implementation
dns = {}dns[‘google.com‘] = ‘74.125.239.133‘dns[‘scribd.com‘] = ‘23.235.47.175‘site = input(‘>>> ‘)print(site,dns.get(site))
>>> google.comgoogle.com 74.125.239.133
Vote
voted = {}def check_voter(name):    if voted.get(name):        print(‘已经投过票‘)    else:        voted[name] = True        print(‘可以投票‘)check_voter(‘Tom‘)check_voter(‘Vic‘)check_voter(‘Tom‘)
可以投票可以投票已经投过票
User Cache
cache = {}def get_page(url):    if cache.get(url):        return chache[url]#返回缓存数据    else:        data = get_data_from_server(url)#默认配置        cache[url] = data        return data
Conflict + Performance Fill factor = existing/all space

Breadth-First search (BFS) Implementation diagram
graph = {}graph[‘you‘] = [‘alice‘,‘bob‘,‘claire‘]graph[‘bob‘] = [‘anuj‘,‘peggy‘]graph[‘alice‘] = [‘peggy‘]graph[‘claire‘]=[‘thom‘,‘jonny‘]graph[‘anuj‘]=[]graph[‘peggy‘]=[]graph[‘thom‘] = []graph[‘jonny‘] = []
Queue
from collections import deque
type(search_queue)
collections.deque
def person_is_seller(name):    return name[-1] == ‘m‘def search(name):    search_queue = deque()#创建对列    global graph    search_queue += graph[name]#从谁开始搜索    searched = []#已经搜索,防止无限循环        while search_queue:#只要队列里有人        person = search_queue.popleft()#取出一人        if person not in searched:            if person_is_seller(person):                print(person+‘ is a mango seller‘)                return True            else:                search_queue+=graph[person]            searched.append(person)    return Falsesearch(‘you‘)
thom is a mango sellerTrue

Dixtra algorithm
有向无环图、加权图(权值为正)
graph = {}graph[‘start‘] = {}graph[‘start‘][‘a‘]=6graph[‘start‘][‘b‘] = 2graph[‘a‘]={}graph[‘a‘][‘fin‘] = 1graph[‘b‘]={}graph[‘b‘][‘a‘]=3graph[‘b‘][‘fin‘]=5graph[‘fin‘] = {}#终点没有邻居
infinity = float("inf")#+oo正无穷costs = {}costs[‘a‘] =6costs[‘b‘] =2costs[‘fin‘] = infinity
parents = {}parents[‘a‘] = ‘start‘parents[‘b‘] = ‘start‘parents[‘fin‘] = None
processed = []#已经处理过的点
  def find_lowest_cost_node (costs): Lowest_cost = float ("inf") Lowest_cost_node = None for node in costs            : #遍历所有节点 cost = costs[node] Global processed if cost<lowest_cost and node not in processed: Lowest_cost = Cost Lowest_cost_node = node return lowest_cost_nodedef get_load (parents,destination): #获得        Path T = parents.get (destination) print (destination, ' <--', end= "") while T:print (T, ' <--', end= "")    t = Parents.get (t) print (' None ') node = Find_lowest_cost_node (costs) while node: #当还有节点可以处理的时候 cost = Costs[node] Neighbors = Graph[node] for n in Neighbors.keys (): New_cost = Cost + neighbors[n] if new_cost < cost S[n]: costs[n] = new_cost Parents[n] = node processed.append (node) node = Find_lowest_ Cost_node (costs) print ("Cost is", costs[' fin ') get_load (parents, ' fin ')  
cost is  6fin <-- a <-- b <-- start <-- None

Greedy algorithm (not necessarily the optimal solution, very close) set operation
fruits = set([‘avocado‘,‘tomato‘,‘banana‘])vegetables = set([‘beets‘,‘carrots‘,‘tomato‘])print(‘|:并集\n\t‘,fruits | vegetables)print(‘&:交集\n\t‘,fruits & vegetables)print(‘-:差集\n\t‘,fruits - vegetables)
|:并集     {‘tomato‘, ‘avocado‘, ‘beets‘, ‘carrots‘, ‘banana‘}&:交集     {‘tomato‘}-:差集     {‘avocado‘, ‘banana‘}
Fuzzy algorithm--set overlay problem
states_needed = set([‘mt‘,‘wa‘,‘or‘,‘id‘,‘nv‘,‘ut‘,‘ca‘,‘az‘])stations = {}stations[‘kone‘] = set([‘id‘,‘nv‘,‘ut‘])stations[‘ktwo‘] = set([‘wa‘,‘id‘,‘mt‘])stations[‘kthree‘] = set([‘or‘,‘nv‘,‘ca‘])stations[‘kfour‘] = set([‘nv‘,‘ut‘])stations[‘kfive‘] = set([‘ca‘,‘az‘])final_stations = set()#最终电台while states_needed:        best_station = None#存放覆盖区域最多的电台    states_covered = set()    for station,states_for_station in stations.items():        covered = states_needed & states_for_station        if len(covered)>len(states_covered):            best_station = station            states_covered = covered                states_needed -= states_covered    final_stations.add(best_station)    del stations[best_station]#用过的删除    print(final_stations)
{‘kfive‘, ‘ktwo‘, ‘kone‘, ‘kthree‘}

Code implementation and improvement of algorithm diagram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.