def recursive_remove(S: ArrayStack): if S.is_empty(): pass else: S.pop() print(S._data) recursive_remove(S)
def reverse_list_by_stack(l: List, s: ArrayStack): for i in l: s.push(i) for i in range(len(l)): if s.is_empty(): break else: l[i] = s.pop() return l
def kuo_hao_match(expr: str): """返回True 如果所有的delimiters 都前后正确匹配,反之返回False""" left = '([{' right = ')]}' happened = ArrayStack() for _chr in expr: if _chr in left: happened.push(_chr) elif _chr in right: if happened.is_empty(): return False if right.index(_chr) != left.index(happened.pop()): return False # 如果能顺利遍历排空所有happened,则返回True return happened.is_empty()
from ADT import ArrayStack from typing import List def reverse_list_by_stack(l: List, s: ArrayStack): for i in l: s.push(i) for i in range(len(l)): if s.is_empty(): break else: l[i] = s.pop() return l if __name__ == "__main__": s = ArrayStack() l = [i for i in range(10)] l = reverse_list_by_stack(l, s) print(l)
def transfer(self, S: ArrayStack, T: ArrayStack): while True: try: T.push(S.pop()) except: break
from ADT import ArrayStack class Solution: def transfer(self, S: ArrayStack, T: ArrayStack): while True: try: T.push(S.pop()) except: break if __name__ == "__main__": s = ArrayStack() t = ArrayStack() for i in range(10): s.push(i) sol = Solution() sol.transfer(S=s, T=t) print(t._data)