preface

From books ： Stack （ Sometimes called “ LIFO stack ”） Is an ordered collection of items , Adding and removing new items always happens at the same end . This end is often called “ Top ”. The end corresponding to the top is called “ bottom
”. The bottom of the stack is important , Because the items near the bottom of the stack have the longest storage time . Recently added items are the first to be removed . This ordering principle is sometimes referred to as LIFO
, Last in, first out . It sorts based on the length of time in the set . The newer item is near the top , The older item is near the bottom .

Stack function

Bracket missing detection using stack
from pythonds.basic.stack import Stack s_dict = {"{":"}","[":"]","(":")"} def
parChecker(symbolString): balance = True s = Stack() for char in symbolString:
if char in "{[(": s.push(char) elif char in "}])": if s.isEmpty():
# In one case, the left bracket is missing , There is no left bracket in the stack balance = False else: if s_dict[s.peek()] == char: s.pop()
# Each consists of a set of brackets , A member pops up on the stack return s.isEmpty() and balance # last , All brackets match , The stack should be empty
print(parChecker('123(((131)))1331')) print(parChecker('([])({})[]'))
print(parChecker('123(((131)))1331')) print(parChecker('([])({})[]')) True True
Process finished with exit code 0
Using stack to realize conversion between decimal system and other systems
from pythonds.basic.stack import Stack def
baseConverter(decNumber:int,base:int): digits = "0123456789ABCDEF" if base > 16
or base < 2: return decNumber # Out of conversion range remstack = Stack() # Store remainder while decNumber
> 0: rem = decNumber % base # Take remainder remstack.push(rem) decNumber = decNumber //
base # Divide by rounding down newString = "" while not remstack.isEmpty() : newString =
newString + digits[remstack.pop()] return newString print(baseConverter(25,2))
print(baseConverter(169,16)) 11001 A9 Process finished with exit code 0

Technology
Daily Recommendation
views 2