def VerifyExpression(sample): stack = Stack() for i in sample: print(f"current element => {i}") # check for open parenthesis if i == '{' or i == '[' or i == '(': stack.push(i) elif i == ')': if stack.peek() == '(': stack.pop() else: print("Not valid") return False elif i == ']': if stack.peek() == '[': stack.pop() else: return False elif i == '}': if stack.peek() == '{': stack.pop() else: return False print('-' * 30) print("Current stack status") stack.Print() print('-' * 30) if stack.peek() != -1: return False else: return True
def Print(self, outputdir, **kwargs): # TODO: Use actual signal and background histograms for the SensitivityScan as # defined in Register instead of what is 'guessed' by Stack.Print. kwargs.setdefault("sensitivity", True) self.CreateHistograms() for varexp, comparator, cutvalue in self._drawcuts: if not varexp in self._binning: continue stack = Stack() for histotype, histos in self._store.items(): for i, histo in histos.items(): stack.Register(histo[varexp, cutvalue]) direction = "-" if "<" in comparator else "+" cutmarkerprops = { "direction": direction, "drawarrow": any([s in comparator for s in "<>"]), } if kwargs.get("sensitivity"): kwargs["direction"] = direction cutmarker = CutMarker(float(cutvalue), **cutmarkerprops) stack.Print(os.path.join( outputdir, self.FormatOutput(varexp=varexp, comparator=comparator, cutvalue=cutvalue), ), xtitle=self._vartitle.get(varexp, varexp), xunits=self._varunits.get(varexp, None), inject0=cutmarker, mkdir=True, **kwargs)
def evaluatePostfix(expression): """ In postfix expression, we start scanning the expression from left to right """ stack = Stack() for i in expression: # check if the current character is an operator or digit if i in operators: # perform the operation op2 = int(stack.pop()) op1 = int(stack.pop()) if i == '+': res = op1 + op2 elif i == '-': res = op1 - op2 elif i == '*': res = op1 * op2 elif i == '/': res = op1 / op2 elif i == '^': res = op1 ** op2 # push the result into the stack stack.push(res) else: stack.push(i) stack.Print() print('-'* 30) # return the element left in stack return stack.peek()
def evaluatePrefix(expression): # create a new stack stack = Stack() # here in this case, we have to start scanning the expression from right for i in expression[::-1]: if i in operators: op1 = int(stack.pop()) op2 = int(stack.pop()) if i == '+': res = op1 + op2 elif i == '-': res = op1 - op2 elif i == '*': res = op1 * op2 elif i == '/': res = op1 / op2 elif i == '^': res = op1 ^ op2 stack.push(res) else: stack.push(i) stack.Print() print('-' * 30) return stack.peek()
def main(): while True: print("1: Reverse a string") print("2: Stop") choice = int(input(">>>")) if choice == 1: string = input("Enter a valid string: ") # creating a new stack instance stack = Stack() # push elements of string in stack for i in range(len(string)): stack.push(string[i]) # print the filled stack stack.Print() # since stack is LIFO structure, we will start popping the last elements from top newstring = '' for i in range(len(string)): newstring += stack.pop() # freeeing the stack del stack print(newstring) else: break
def main(): # create a new instance of stack for performing operations stack = Stack() while (True): print("1: Push") print("2: Pop") print("3: Peek") print("4: Print Stack") print("5: STOP") choice = int(input(">>>")) if choice == 1: element = int(input("Enter element to be inserted: ")) stack.push(element) elif choice == 2: elem = stack.pop() print("Popped element: ", elem) elif choice == 3: elem = stack.peek() print("Element at top is: ", elem) elif choice == 4: stack.Print() else: break