Detecting whether a word is a word reversed letters
If # is detected word is the word of reversed letters DEF Anagram (S1, S2): num1 = [0] * 26 is num2 = [0] * 26 is for I in Range (len (S1)): POS = the ord (S1 [ I]) - the ord ( 'A') num1 [POS] + =. 1 for I in Range (len (S2)): POS = the ord (S2 [I]) - the ord ( 'A') num2 [POS] + = . 1 J = 0 State = True the while J <26 is and State: IF num1 [J] = num2 [J]:! State = False BREAK J + =. 1 return State the while True: A = iNPUT ( 'enter word:') b = input ( 'enter word:') answer = Anagram (A, B) Print (answer)
Implementation of a stack (as the end of the list in the stack) complexity is 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())
Implementation of the two stacks (stack a list of head-end) complexity is O (n)
Word is detected whether # inverted letter word class Stack (): DEF the __init __ (Self): Print ( 'stack initialization') self.items = [] DEF isEmpty (Self): return self.items == [] DEF Push (Self, Item): Print ( 'stack:', Item) self.items.insert (0, Item) DEF POP (Self): RES = self.items.pop (0) Print ( "the stack: ' , RES) return RES DEF PEEK (Self): RES = self.items [len (self.items) -. 1] Print ( 'the top element', RES) return RES DEF size (Self): RES = len (Self. items) Print ( 'stack size:', 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()
Application (matching brackets)
Word is detected whether # inverted letter word class Stack (): DEF the __init __ (Self): Print ( 'stack initialization') self.items = [] DEF isEmpty (Self): return self.items == [] DEF Push (Self, Item): Print ( 'stack:', Item) self.items.insert (0, Item) DEF POP (Self): RES = self.items.pop (0) Print ( "the stack: ' , RES) return RES DEF PEEK (Self): RES = self.items [len (self.items) -. 1] Print ( 'the top element', RES) return RES DEF size (Self): RES = len (Self. items) Print ( 'stack size:', RES) return RES DEF parcheck (STR): Stack = S () Mark = True index = 0 the while index <len (STR) and Mark: Symbol STR = [index] # is pressed into left parenthesis IF Symbol == '(': s.push (Symbol) # string case the right parenthesis, the stack throw a left parenthesis the else: IF s.isempty (): Mark = False the else: s.pop () index. 1 = + IF == True and s.isempty Mark (): Print ( 'match' ) return True the else: Print ( 'mismatch') return False # parcheck ( '()') # parcheck ( '()))') the while True: S = iNPUT ( 'enter test brackets' ").strip() parcheck (s)
Universal matching brackets
Application of the decimal to binary conversion
Stack class (): DEF the __init __ (Self): Print ( 'stack initialization') self.items = [] DEF isEmpty (Self): return self.items == [] DEF Push (Self, Item): Print ( 'into stack: ', Item) self.items.insert (0, Item) DEF POP (Self): RES = self.items.pop (0) Print (' the stack: ', RES) return RES DEF PEEK (Self): self.items = RES [len (self.items) -. 1] Print ( 'the top element', RES) return RES DEF size (Self): RES = len (self.items) Print ( "stack size: ' RES) return RES DEF divideby2 (NUM): S = Stack () = Result '' NUM = int (NUM) the while NUM> 0: # remainder Q = 2% NUM s.push (Q) # commercially NUM = NUM 2 // the while s.isempty Not (): # output result = result + STR (s.pop ()) return Result the while True: . S = iNPUT ( 'enter to convert the decimal number>') Strip () Result = divideby2 (S) Print (Result)
------------ ------------ restore content begins
Detecting whether a word is a word reversed letters
If # is detected word is the word of reversed letters DEF Anagram (S1, S2): num1 = [0] * 26 is num2 = [0] * 26 is for I in Range (len (S1)): POS = the ord (S1 [ I]) - the ord ( 'A') num1 [POS] + =. 1 for I in Range (len (S2)): POS = the ord (S2 [I]) - the ord ( 'A') num2 [POS] + = . 1 J = 0 State = True the while J <26 is and State: IF num1 [J] = num2 [J]:! State = False BREAK J + =. 1 return State the while True: A = iNPUT ( 'enter word:') b = input ( 'enter word:') answer = Anagram (A, B) Print (answer)
Implementation of a stack (as the end of the list in the stack) complexity is 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())
Implementation of the two stacks (stack a list of head-end) complexity is O (n)
Word is detected whether # inverted letter word class Stack (): DEF the __init __ (Self): Print ( 'stack initialization') self.items = [] DEF isEmpty (Self): return self.items == [] DEF Push (Self, Item): Print ( 'stack:', Item) self.items.insert (0, Item) DEF POP (Self): RES = self.items.pop (0) Print ( "the stack: ' , RES) return RES DEF PEEK (Self): RES = self.items [len (self.items) -. 1] Print ( 'the top element', RES) return RES DEF size (Self): RES = len (Self. items) Print ( 'stack size:', 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()
Application (matching brackets)
Word is detected whether # inverted letter word class Stack (): DEF the __init __ (Self): Print ( 'stack initialization') self.items = [] DEF isEmpty (Self): return self.items == [] DEF Push (Self, Item): Print ( 'stack:', Item) self.items.insert (0, Item) DEF POP (Self): RES = self.items.pop (0) Print ( "the stack: ' , RES) return RES DEF PEEK (Self): RES = self.items [len (self.items) -. 1] Print ( 'the top element', RES) return RES DEF size (Self): RES = len (Self. items) Print ( 'stack size:', RES) return RES DEF parcheck (STR): Stack = S () Mark = True index = 0 the while index <len (STR) and Mark: Symbol STR = [index] # is pressed into left parenthesis IF Symbol == '(': s.push (Symbol) # string case the right parenthesis, the stack throw a left parenthesis the else: IF s.isempty (): Mark = False the else: s.pop () index. 1 = + IF == True and s.isempty Mark (): Print ( 'match' ) return True the else: Print ( 'mismatch') return False # parcheck ( '()') # parcheck ( '()))') the while True: S = iNPUT ( 'enter test brackets' ").strip() parcheck (s)
Universal matching brackets
Application of the decimal to binary conversion
Stack class (): DEF the __init __ (Self): Print ( 'stack initialization') self.items = [] DEF isEmpty (Self): return self.items == [] DEF Push (Self, Item): Print ( 'into stack: ', Item) self.items.insert (0, Item) DEF POP (Self): RES = self.items.pop (0) Print (' the stack: ', RES) return RES DEF PEEK (Self): self.items = RES [len (self.items) -. 1] Print ( 'the top element', RES) return RES DEF size (Self): RES = len (self.items) Print ( "stack size: ' RES) return RES DEF divideby2 (NUM): S = Stack () = Result '' NUM = int (NUM) the while NUM> 0: # remainder Q = 2% NUM s.push (Q) # commercially NUM = NUM 2 // the while s.isempty Not (): # output result = result + STR (s.pop ()) return Result the while True: . S = iNPUT ( 'enter to convert the decimal number>') Strip () Result = divideby2 (S) Print (Result)
Application (infix expression turn suffix)
Code
Word is detected whether # inverted letter word class Stack (): DEF the __init __ (Self): Print ( 'stack initialization') self.items = [] DEF isEmpty (Self): return self.items == [] DEF Push (Self, Item): Print ( 'stack:', Item) self.items.insert (0, Item) DEF POP (Self): RES = self.items.pop (0) Print ( "the stack: ' , RES) return RES DEF PEEK (Self): RES = self.items [len (self.items) -. 1] Print ( 'the top element', RES) return RES DEF size (Self): RES = len (Self. items) Print ( "stack size: 'res) return res def infixtopostfix(goals): # Operator precedence prec = {} prec [ '*'] =. 3 prec [ '/'] =. 3 prec [ '+'] = 2 prec [ '-'] = 2 prec [ '('] =. 1 # operator stack opstack = stack () postfixlist = [] # here the bad FIG goallist = List (Goals) for in goallist Goal: IF Goal in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'or Goal in' 0123456789 ': postfixlist.append (Goal) elif == Goal '(': opstack.push (Goal) elif Goal == ')': topgoal = opstack.pop () the 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)
Application (postfix expression)
Code
Word is detected whether # inverted letter word class Stack (): DEF the __init __ (Self): Print ( 'stack initialization') self.items = [] DEF isEmpty (Self): return self.items == [] DEF Push (Self, Item): Print ( 'stack:', Item) self.items.insert (0, Item) DEF POP (Self): RES = self.items.pop (0) Print ( "the stack: ' , RES) return RES DEF PEEK (Self): RES = self.items [len (self.items) -. 1] Print ( 'the top element', RES) return RES DEF size (Self): RES = len (Self. items) Print ( "stack size: 'res) return res def infixtopostfix(goals): # Operator precedence prec = {} prec [ '*'] =. 3 prec [ '/'] =. 3 prec [ '+'] = 2 prec [ '-'] = 2 prec [ '('] =. 1 # operator stack opstack = stack () postfixlist = [] # here the bad FIG goallist = List (Goals) for in goallist Goal: IF Goal in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'or Goal in' 0123456789 ': postfixlist.append (Goal) elif == Goal '(': opstack.push (Goal) elif Goal == ')': topgoal = opstack.pop () the 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 () express list = list (express) print (express list) for i in express list: 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 the else: return OP1-OP2 the while True: S = iNPUT ( 'enter the expression to be converted>') Strip (). RESULT1 = infixtopostfix (S) Print (RESULT1) result2 postfixeval = ( result1) Print (result2)