def is_balanced(input_str): mys = ArrayStack() lst = input_str.split() fore = "([{" back = ")]}" for i in range(len(lst)): if lst[i] in fore: myStack.push(lst[i]) else: if lst[i] not in back: raise Exception("invalid input!") else: lst[i] = mine if mys.is_empty() == False: curr = myStack.pop() else: return False if curr == '(' and mine != ')': #mind == syntax!!! return False elif curr == '[' and mine != ']': return False elif curr == '{' and mine != '}': return False if mys.is_empty() == False: return False return True
class MaxStack: def __init__(self): self._data = ArrayStack() def is_empty(self): return len(self._data) == 0 def __len__(self): return len(self._data) def push(self, e): if len(self._data) == 0: elem = (e, e) self._data.push(elem) else: last = self._data.top() mymax = last[1] currmax = mymax if e > mymax: currmax = e tup1 = (e, currmax) self._data.push(tup1) def top(self): if self.is_empty(): raise Empty("MaxStack is empty") ##return a reference without removing it temp = self._data.top() return temp[0] def pop(self): if self.is_empty(): raise Empty("MaxStack is empty") temp = self._data.top() self._data.pop() ##Can I directly operate on the LL array?? return temp[0] def max(self): ## theta 1 worst-case time if self.is_empty(): raise Empty("MaxStack is empty") temp = self._data.top() return temp[1]
def eval_postfix(exp_str): str_lst = exp_str.split(" ") operators = "+-*/" mystack = ArrayStack() for item in str_lst: if item not in operators: mystack.push(int(item)) #KEY is to convert to int!!! else: arg2 = mystack.pop() arg1 = mystack.pop() #ARG2 popped first!!! if item == '+': res = arg1 + arg2 elif item == '-': res = arg1 - arg2 elif item == '*': res = arg1 * arg2 else: res = arg1 / arg2 mystack.push(res) return mystack.pop()
def permutations(lst): ## non-recursive ## alloed a Stack, a Queue, Theta 1 additional space # use stack to generate permutations, use queue to store partial collection of pers generated so far # queue set up to hold mystack = ArrayStack() length = math.factorial(len(lst)) result = ArrayQueue(length) for i in range(len(lst)): mystack.push(lst[i]) lst1 = [mystack.pop()] result.enqueue(lst1) while mystack.is_empty() == False: elem = mystack.pop() times = math.factorial(len(result.first())) for i in range(times): lst1 = result.dequeue() for i in range(len(lst1) + 1): newlst = copy.deepcopy(lst1) newlst.insert(i, elem) result.enqueue(newlst) return result
def eval_prefix(exp_str): str_lst = exp_str.split(" ") operators = "+-*/" mystack = ArrayStack() count1 = 0 count2 = 0 count3 = 0 for i in range(len(str_lst)): # for i in range if count2 != 0 and count1 != 0: arg2 = mystack.pop() arg1 = mystack.pop() item = mystack.pop() if item == '+': res = arg1 + arg2 elif item == '-': res = arg1 - arg2 elif item == '*': res = arg1 * arg2 else: res = arg1 / arg2 mystack.push(res) count1 = 0 count3 = 1 if (count1 != 0) and (str_lst[i] not in operators) and (count2 == 0): continue if (count3 != 0): count3 = 0 continue if str_lst[i] in operators: mystack.push(str_lst[i]) count1 = 0 else: arg1 = int(str_lst[i]) ##CONVERT to int!! if count2 == 0 and count1 == 0: arg2 = int(str_lst[i+1]) count1 += 1 item = mystack.pop() if item == '+': res = arg1 + arg2 elif item == '-': res = arg1 - arg2 elif item == '*': res = arg1 * arg2 else: res = arg1 / arg2 mystack.push(res) if (i+2 <= len(str_lst)-1): if str_lst[i+2] not in operators: count2 += 1 else: arg2 = arg1 arg1 = int(mystack.pop()) item = mystack.pop() if item == '+': res = arg1 + arg2 elif item == '-': res = arg1 - arg2 elif item == '*': res = arg1 * arg2 else: res = arg1 / arg2 mystack.push(res) return res
def __init__(self): self._data = ArrayStack()