python数据结构(1)
检测单词是否是由颠倒字母组成的词
#检测单词是否是由颠倒字母组成的词 def anagram(s1,s2): num1=[0]*26 num2=[0]*26 for i in range(len(s1)): pos=ord(s1[i])-ord('a') num1[pos]+=1 for i in range(len(s2)): pos = ord(s2[i]) - ord('a') num2[pos]+= 1 j=0 state=True while j<26 and state: if num1[j]!=num2[j]: state=False break j += 1 return state while True: a=input('请输入单词:') b=input('请输入单词:') answer=anagram(a,b) print(answer)
栈的实现方法一(以列表末尾为栈顶)复杂度为O(1)
class stack(): def __init__(self): self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.append(item) def pop(self): res=self.items.pop() print('出栈:',res) return res def peek(self): return self.items[len(self.items)-1] def size(self): print('栈的大小:') return len(self.items) l=stack() l.push('1') l.push('2') l.push('3') l.push('4') print(l.size()) res=l.pop() print(res) print(l.size())
栈的实现方法二(以列表首端为栈顶)复杂度为O(n)
#检测单词是否是由颠倒字母组成的词 class stack(): def __init__(self): print('栈初始化') self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.insert(0,item) def pop(self): res=self.items.pop(0) print('出栈:',res) return res def peek(self): res=self.items[len(self.items) - 1] print('栈顶元素',res) return res def size(self): res=len(self.items) print('栈的大小:',res) return res l=stack() l.size() l.push('1') l.push('2') l.push('3') l.push('4') l.size() res=l.pop() l.size()
应用(括号匹配)
#检测单词是否是由颠倒字母组成的词 class stack(): def __init__(self): print('栈初始化') self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.insert(0,item) def pop(self): res=self.items.pop(0) print('出栈:',res) return res def peek(self): res=self.items[len(self.items) - 1] print('栈顶元素',res) return res def size(self): res=len(self.items) print('栈的大小:',res) return res def parcheck(str): s=stack() mark=True index=0 while index<len(str) and mark: symbol=str[index] #左括号压入 if symbol=='(': s.push(symbol) #字符串遇到右括号,栈里抛出一个左括号 else: if s.isempty(): mark=False else: s.pop() index+=1 if mark==True and s.isempty(): print('匹配') return True else: print('不匹配') return False # parcheck('()') # parcheck('()))') while True: s=input('请输入括号测试》').strip() parcheck(s)
通用括号匹配
应用将10进制数转化为2进制
class stack(): def __init__(self): print('栈初始化') self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.insert(0,item) def pop(self): res=self.items.pop(0) print('出栈:',res) return res def peek(self): res=self.items[len(self.items) - 1] print('栈顶元素',res) return res def size(self): res=len(self.items) print('栈的大小:',res) return res def divideby2(num): s=stack() result = '' num=int(num) while num>0: #余数 q=num%2 s.push(q) #商 num=num//2 while not s.isempty(): #输出结果 result=result+ str (s.pop()) return result while True: s=input('请输入要转换的十进制数>').strip() result=divideby2(s) print(result)
------------恢复内容开始------------
检测单词是否是由颠倒字母组成的词
#检测单词是否是由颠倒字母组成的词 def anagram(s1,s2): num1=[0]*26 num2=[0]*26 for i in range(len(s1)): pos=ord(s1[i])-ord('a') num1[pos]+=1 for i in range(len(s2)): pos = ord(s2[i]) - ord('a') num2[pos]+= 1 j=0 state=True while j<26 and state: if num1[j]!=num2[j]: state=False break j += 1 return state while True: a=input('请输入单词:') b=input('请输入单词:') answer=anagram(a,b) print(answer)
栈的实现方法一(以列表末尾为栈顶)复杂度为O(1)
class stack(): def __init__(self): self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.append(item) def pop(self): res=self.items.pop() print('出栈:',res) return res def peek(self): return self.items[len(self.items)-1] def size(self): print('栈的大小:') return len(self.items) l=stack() l.push('1') l.push('2') l.push('3') l.push('4') print(l.size()) res=l.pop() print(res) print(l.size())
栈的实现方法二(以列表首端为栈顶)复杂度为O(n)
#栈的实现方法二 class stack(): def __init__(self): print('栈初始化') self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.insert(0,item) def pop(self): res=self.items.pop(0) print('出栈:',res) return res def peek(self): res=self.items[len(self.items) - 1] print('栈顶元素',res) return res def size(self): res=len(self.items) print('栈的大小:',res) return res l=stack() l.size() l.push('1') l.push('2') l.push('3') l.push('4') l.size() res=l.pop() l.size()
应用(括号匹配)
#括号匹配 class stack(): def __init__(self): print('栈初始化') self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.insert(0,item) def pop(self): res=self.items.pop(0) print('出栈:',res) return res def peek(self): res=self.items[len(self.items) - 1] print('栈顶元素',res) return res def size(self): res=len(self.items) print('栈的大小:',res) return res def parcheck(str): s=stack() mark=True index=0 while index<len(str) and mark: symbol=str[index] #左括号压入 if symbol=='(': s.push(symbol) #字符串遇到右括号,栈里抛出一个左括号 else: if s.isempty(): mark=False else: s.pop() index+=1 if mark==True and s.isempty(): print('匹配') return True else: print('不匹配') return False # parcheck('()') # parcheck('()))') while True: s=input('请输入括号测试》').strip() parcheck(s)
通用括号匹配
应用将10进制数转化为2进制
class stack(): def __init__(self): print('栈初始化') self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.insert(0,item) def pop(self): res=self.items.pop(0) print('出栈:',res) return res def peek(self): res=self.items[len(self.items) - 1] print('栈顶元素',res) return res def size(self): res=len(self.items) print('栈的大小:',res) return res def divideby2(num): s=stack() result = '' num=int(num) while num>0: #余数 q=num%2 s.push(q) #商 num=num//2 while not s.isempty(): #输出结果 result=result+ str (s.pop()) return result while True: s=input('请输入要转换的十进制数>').strip() result=divideby2(s) print(result)
应用(表达式中缀转后缀)
代码实现
#表达式中缀转后缀 class stack(): def __init__(self): print('栈初始化') self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.insert(0,item) def pop(self): res=self.items.pop(0) print('出栈:',res) return res def peek(self): res=self.items[len(self.items) - 1] print('栈顶元素',res) return res def size(self): res=len(self.items) print('栈的大小:',res) return res def infixtopostfix(goals): #操作符优先级 prec={} prec['*'] = 3 prec['/'] = 3 prec['+'] = 2 prec['-'] = 2 prec['('] = 1 #操作符栈 opstack=stack() postfixlist=[] #上图此处有误 goallist=list(goals) for goal in goallist: if goal in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'or goal in '0123456789': postfixlist.append(goal) elif goal=='(': opstack.push(goal) elif goal==')': topgoal=opstack.pop() while topgoal !='(': postfixlist.append(topgoal) topgoal=opstack.pop() else: while (not opstack.isempty()) and (prec[opstack.peek()] >= prec[goal]): postfixlist.append(opstack.pop()) opstack.push(goal) while not opstack.isempty(): postfixlist.append(opstack.pop()) return ''.join(postfixlist) while True: s=input('请输入要转换的表达式>').strip() result=infixtopostfix(s) print(result)
应用(后缀表达式求值)
代码实现
#后缀表达式求值 class stack(): def __init__(self): print('栈初始化') self.items=[] def isempty(self): return self.items==[] def push(self,item): print('入栈:',item) self.items.insert(0,item) def pop(self): res=self.items.pop(0) print('出栈:',res) return res def peek(self): res=self.items[len(self.items) - 1] print('栈顶元素',res) return res def size(self): res=len(self.items) print('栈的大小:',res) return res def infixtopostfix(goals): #操作符优先级 prec={} prec['*'] = 3 prec['/'] = 3 prec['+'] = 2 prec['-'] = 2 prec['('] = 1 #操作符栈 opstack=stack() postfixlist=[] #上图此处有误 goallist=list(goals) for goal in goallist: if goal in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'or goal in '0123456789': postfixlist.append(goal) elif goal=='(': opstack.push(goal) elif goal==')': topgoal=opstack.pop() while topgoal !='(': postfixlist.append(topgoal) topgoal=opstack.pop() else: while (not opstack.isempty()) and (prec[opstack.peek()] >= prec[goal]): postfixlist.append(opstack.pop()) opstack.push(goal) while not opstack.isempty(): postfixlist.append(opstack.pop()) return ''.join(postfixlist) def postfixeval(express): opertostack=stack() expresslist=list(express) print(expresslist) for i in expresslist: if i in '0123456789': opertostack.push(int(i)) else: oper2 = opertostack.pop() oper1=opertostack.pop() result=domath(i,oper1,oper2) opertostack.push(result) return opertostack.pop() def domath(op,op1,op2): if op=='*': return op1*op2 elif op=='/': return op1/op2 elif op=='+': return op1+op2 else : return op1-op2 while True: s=input('请输入要转换的表达式>').strip() result1 = infixtopostfix(s) print(result1) result2=postfixeval(result1) print(result2)