算法

1,选择排序

#选择排序
def select_sort(arr):
    for start in range(len(arr)-1):
        max_index=start
        for i in range(start,len(arr)):
            if arr[max_index]<arr[i]:
                max_index=i
        arr[max_index],arr[start]=arr[start],arr[max_index]

arr=[1,2,3,4,6,1,32,9]
select_sort(arr)
print(arr)

 

2,回文

#回文
from collections import deque
def hw(s):
    dq=deque(s)
    while len(dq)>1:
        if dq.popleft()!=dq.pop():
            return False
    return True
s="radar"
print(hw(s))

 

3,表达式的前缀

#前缀
def mid2prefix(exp):
    exp=exp[::-1]
    OPRS='()+-*/'
    ans=""
    stack=[]
    for e in exp:
        if e in OPRS:
            if not stack or e==')':
                stack.append(e)
            elif e=='(':   #去掉左右括号
                while stack[-1]!=')':
                    ans=ans+stack.pop()
                stack.pop()
            elif OPRS.index(e)>=OPRS.index(stack[-1])//2:  #+-或*/优先级相等
                stack.append(e)
            else:
                while stack and OPRS.index(e)//2<OPRS.index(stack[-1])//2:
                    ans=ans+stack.pop()
                stack.append(e)
        else:
            ans=ans+e
    while stack:
        ans=ans+stack.pop()
    return ans[::-1]

s='4+(2+3)*2'
print(mid2prefix(s))

 

4,后缀表达式

#后缀
def mid2prefix(exp):
    OPRS='()+-*/'
    ans=""
    stack=[]
    for e in exp:
        if e in OPRS:
            if not stack or e=='(':
                stack.append(e)
            elif e==')':   #去掉左右括号
                while stack[-1]!='(':
                    ans=ans+stack.pop()
                stack.pop()
            elif OPRS.index(e)>=OPRS.index(stack[-1])//2:  #+-或*/优先级相等
                stack.append(e)
            else:
                while stack and OPRS.index(e)//2<OPRS.index(stack[-1])//2:
                    ans=ans+stack.pop()
                stack.append(e)
        else:
            ans=ans+e
    while stack:
        ans=ans+stack.pop()
    return ans

s='4+2+3*2'
print(mid2prefix(s))

 

5,前缀表达式的还原计算

#前缀表达式的还原计算
def caic_prefix(exp):
    stack=[]
    exp=exp[::-1]
    ans=0
    for e in exp:
        if e not in '+-*/':
            stack.append(e)
        else:
            ans=eval(stack.pop()+e+stack.pop())
            stack.append(str(ans))

    return stack[-1]
    return eval(stack[-1])


s='/*++32156'
print(caic_prefix(s))

 

6,哈希表

#集合与哈希表的使用
#集合
def find_repeat(s):
    s1=set(s)
    return len(s)!=len(s1)
s='assd'
print(find_repeat(s))

#哈希表
def find_repeat_dict(s):
    ht={}
    for c in s:
        if c in ht: #有重复的就放回True
            ht[c]=ht[c]+1
            return True
        else:
            ht[c]=1
    return False
s='aabbcd'
print(find_repeat_dict(s))

 

7,找出相反数的对数

#相反数
def count_matches(s):
    ht={}
    for c in s:
        c=abs(c)
        if c in ht:
            ht[c]=ht[c]+1
        else:
            ht[c]=1
    return len([x for x in ht.values() if x == 2])

s='1,2,3,-1,-2,-3'
print(count_matches(s))

8,列表中的2数之和等于给定的值

#两数之和

def sum(arr,target):
    ht=dict()
    for i,e in enumerate(arr):   #i表示遍历下标,e表示遍历值
        if target-e in ht:
            return (i,ht[target-e])
        else:
            ht[e]=i

s=[2,7,11,15]
b=9
print(sum(s,b))

 

9,找出列表中两数之和等于和给定数的两数的下标

#找出列表中两数之和等于和给定数的两数的下标
def sum(arr,target):
    ht=dict()  #创建一个空字典
    for i,e in enumerate(arr):   #i表示遍历下标,e表示遍历值,enumerate表示一边遍历一边转换为字典
        if target-e in ht:
            return (i,ht[target-e])
        else:
            ht[e]=i

s=[2,7,11,15]
b=9
print(sum(s,b))



 


10,优先队列,找出第k小的数

 



#优先队列,找出第K小的数
from queue import PriorityQueue
def get_k_min(arr,k):
    pq=PriorityQueue()
    for e in arr:
        pq.put(e)
    while k>0:
        elem=pq.get()
        k=k-1
    return elem
    #print(pq.queue)
    #print(pq.get())#输出最小的一个数,按照最小的出队

arr=[12,17,11,15]
print(get_k_min(arr,3))


 


 

11,利用哈希表查找最小字符串

 

#利用哈希表查找最小字符串
def longest_substr_1(s):
    if not s:
        return 0
    start=0
    end=0
    ht={s[0]:0} #haxibiao
    max_sub=1
    for i in range(1, len(s)):  # range方位为左闭右开
        if s[i] in ht:
            start = max(ht[s[i]] + 1, start)
            ht[s[i]] = i
        else:
            ht[s[i]] = i
        end = i
        max_sub = max(max_sub, end - start + 1)
    return max_sub
print(longest_substr_1('abcabcdab'))


 

12,利用列表查找最小字符串

#利用列表查找最小字符串
from queue import Queue
def longest_substr_2(s):
    q=Queue()
    max_substr=0
    for e in s:
        while e in q:
            q.get()
        q.put(e)
        max_substr=max(max_substr,len(q))
    return max_substr
print(longest_substr_2('abcabcdab'))

13

 

#请选择一个合适的数据结构MyData,尽可能高效的实现元素的插入,删除,按值查找和按序号查找(假设元素值各有不相同)
#*提示:可以使用列表和字典来组合实现
import pygame
class MyData:
    def change(a): #以元组的方式输出序列,并且转换为列表
        b=list(a)
        return b  #返回列表
    def Insert(a,B):      #插入新元素
        m=MyData.change(a)
        b=m.append(B)
        b=m
        print(b)
    def delete(a,B):      #删除新元素
        m=MyData.change(a)
        b=m.remove(B)
        b=m
        print(b)
    def value_off(a,B):      #按值查找
        m=MyData.change(a)
        q=-1
        for i in m:
            q=q+1
            if B==i:
                print(q)
    def index_off(a,B):      #按照序号查找新元素
        m=MyData.change(a)
        print(m[B])

if __name__ == '__main__':
    a=1,2,3,4,5,6
    MyData.Insert(a, 1)
    MyData.delete(a, 1)
    MyData.value_off(a, 2)
    MyData.index_off(a, 5)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值