def eval(self): s = ArrayStack() for item in self._ps: if self._is_number(item): s.push(item) elif self._is_operator(item): right = s.pop() left = s.pop() whole = left + item + right s.push(str(eval(whole))) elif self._is_whitespace(item): pass else: raise PostfixError("Wrong postfix expression!") return s.pop()
class Queue(object): """ Queue impelemented based on stack.""" def __init__(self): self._stack = ArrayStack() self._rstack = ArrayStack() self._len = 0 self._first = None def enqueue(self, e): """ Add an element to the end of the queue.""" self._stack.push(e) if self._len == 0: self._first = e self._len += 1 def dequeue(self): """ Remove the first element of the queue. if the _rstack is not empty, then directly pop one from the _rstack. if empty, _copy_to_stack first and then pop one from _rstack. """ if self._len == 0: raise Empty('Queue is empty!') elif len(self._rstack) == 0: self._copy_to_rstack() self._len -= 1 first = self._rstack.pop() # update first if len(self._rstack) == 0: self._copy_to_rstack() if self._len > 0: self._first = self._rstack.top() else: self._first = None return first def first(self): """ Get the first element of the queue.""" if self._len == 0: raise Empty('Queue is empty!') else: return self._first def _copy_to_rstack(self): """ Pop elements from _stack and push them to _rstack one by one.""" for i in range(self._len): e = self._stack.pop() self._rstack.push(e) def __len__(self): return self._len def is_empty(self): """ Check if the queue is empty or not.""" return self._len == 0
def is_matched_html(raw): """Return True if all HTML tags are properly match; False otherwise.""" S = ArrayStack() j = raw.find('<') # find first '<' character (if any) while j != -1: k = raw.find('>', j + 1) # find next '>' character if k == -1: return False # invalid tag tag = raw[j + 1:k].split(" ")[0] # strip away < > if not tag.startswith('/'): # this is opening tag S.push(tag) else: # this is closing tag if S.is_empty(): return False # nothing to match with if tag[1:] != S.pop(): return False # mismatched delimiter j = raw.find('<', k + 1) # find next '<' character (if any) return S.is_empty() # were all opening tags matched?
# -*- coding: utf-8 -*- from array_stack_c6_16 import ArrayStack from queue import MyQueue def find_element(stack, e): rlt = False q = MyQueue() for i in range(len(stack)): item = stack.pop() if item == e: rlt = True q.enqueue(item) [stack.push(q.dequeue()) for i in range(len(q))] [q.enqueue(s.pop()) for i in range(len(stack))] [stack.push(q.dequeue()) for i in range(len(q))] return rlt if __name__ == '__main__': s = ArrayStack() for i in range(10): s.push(i) print('before: ', s._data) e = 5 flag = find_element(s, e) print('{0:d} is in stack'.format(e)) print('after: ', s._data)
def __init__(self): self._stack = ArrayStack() self._rstack = ArrayStack() self._len = 0 self._first = None
class Deque(object): """ Deque impelemented based on stack. Asyptotic complexity: add_first: O(1) add_last: O(1) delete_first: O(1) amortized delete_last: O(1) amortized first: O(1) last: O(1) len: O(1) is_empty:O(1) """ def __init__(self): self._lstack = ArrayStack() self._rstack = ArrayStack() self._len = 0 self._first = None self._last = None def add_first(self, e): self._lstack.push(e) self._len += 1 self._first = e def add_last(self, e): self._rstack.push(e) self._len += 1 self._last = e def delete_first(self): if self._len == 0: raise Empty('Deque is empty!') elif len(self._lstack) == 0: self._copy_stack(self._rstack, self._lstack) first = self._lstack.pop() self._len -= 1 # update first if len(self._lstack) == 0: self._copy_stack(self._rstack, self._lstack) if self._len > 0: self._first = self._lstack.top() return first def delete_last(self): if self._len == 0: raise Empty('Deque is empty!') elif len(self._rstack) == 0: self._copy_stack(self._lstack, self._rstack) last = self._rstack.pop() self._len -= 1 # update last if len(self._rstack) == 0: self._copy_stack(self._lstack, self._rstack) if self._len > 0: self._last = self._rstack.top() return last def first(self): if self._len == 0: raise Empty('Deque is empty!') else: return self._first def last(self): if self._len == 0: raise Empty('Deque is empty!') else: return self._last def _copy_stack(self, src, dst): for i in range(len(src)): e = src.pop() dst.push(e) def __len__(self): return self._len def is_empty(self): """ Check if the queue is empty or not.""" return self._len == 0
def permutation_stack(l, s): """ permutation by using nonrecursive method, but with a stack.""" length = len(l) # initialize the stack for i in range(length): s.push(l[i:i+1]) s.push(l[:i] + l[i+1:]) rlt = [] # pop two element every time while(not s.is_empty()): top = s.pop() snd = s.pop() if len(top) > 1: for i in range(len(top)): s.push(snd + top[i:i+1]) s.push(top[:i] + top[i+1:]) else: rlt.append(snd + top) return rlt if __name__ == '__main__': l = [i for i in range(1, 4)] s = ArrayStack() pr = permutation_recursive(l) print("pr length: {0:d}".format(len(pr))) print(pr) ps = permutation_stack(l, s) print("ps length: {0:d}".format(len(ps))) print(ps)
def subsets_stack(l): rlt = [] s = ArrayStack() q = MyQueue() s.push([]) s.push(l[0:1]) for i in range(1, len(l)): while (not s.is_empty()): st = s.pop() q.enqueue(copy(st)) st.append(l[i]) q.enqueue(st) while (not q.is_empty()): st = q.dequeue() s.push(st) while (not s.is_empty()): st = s.pop() if st != []: rlt.append(st) return rlt