def conv2hex(decD, dec_string): """ This will take in a decimal value in integer form and convert and return its hexadecimal value. """ value = dec_string new_stack = stack.getStack() while (value % 16) != 0: stack.push(new_stack, decD[value % 16]) value //= 16 final = '' while stack.isEmpty(new_stack) == False: final += str(stack.pop(new_stack)) return final
def main(): import stack mystack = stack.getStack() for item in range(1, 5): stack.push(mystack, item) print('Pushing', item, 'on stack') peek = stack.peek( mystack, (int(input('Enter an element you want to peek into the stack: ')))) if peek == True: print('Element is in stack') else: print('Element is not in stack') while not stack.isEmpty(mystack): item = stack.pop(mystack) print('Popping', item, 'from stack')
def palindromeChecker(s): """ Returns True if the provided string is a palindrome. Uses the stack Module (added in this directory) and the Let's Apply it section from the chapter. """ import stack # init char_stack = stack.getStack() while s != '': if len(s) == 1: return True else: # init is_palindrome = True # to handle strings of odd length compare_length = len(s) // 2 # push second half of input string on stack for k in range(compare_length, len(s)): stack.push(char_stack, s[k]) # pop chars and compare to first half of string k = 0 while k < compare_length and is_palindrome: ch = stack.pop(char_stack) if s[k].lower() != ch.lower(): is_palindrome = False k = k + 1 # return values if is_palindrome: return True else: return False
# import stack import stack # define RPN_stack RPN_stack = stack.getStack() while True: # user input RPN = input("Enter an RPN expression: ") # when user input 'q', program quits if RPN == 'q': break RPN_list = RPN.split() state = "Progress" i = 0 num1 = 0 num2 = 0 result = 0 equal_flag = False # push the number to the stack while state == "Progress" and i < len(RPN_list): if RPN_list[i] == '+' or RPN_list[i] == '*' or RPN_list[ i] == '-' or RPN_list[i] == '/' or RPN_list[i] == '=': if RPN_list[i] == '=': equal_flag = True if len(RPN_stack) == 1 and i == len(RPN_list) - 1: result = float(stack.pop(RPN_stack)) else:
inp = input('Enter an RPN expression: ') if inp == 'q': #quit program more = False elif inp == '': print('Evaluation error') sys.exit(1) else: #split list by ' ' inp_list = inp.split() #convert operands into float for i in range(len(inp_list)): if not is_Operator(inp_list[i]): inp_list[i] = float(inp_list[i]) #make stack s = stack.getStack() #intialize evaluation result variable evaluation = 0 #evaluate expression for c in inp_list: #is an operator if is_Operator(c): if c == '=': evaluation = stack.pop(s) if evaluation == None: #stack is empty print('Evaluation error') sys.exit(1) #exit program elif c == '*': o2 = stack.pop(s)
# Repeat Infinitely while True: # Init # get user input ans = input('Enter an RPN expression:') # terminate if ans is 'q' if ans == 'q': sys.exit() # split ans into list and assign itself ans = ans.split() # generate mainStack mainStack = stack.getStack() # Pre Malformed Detection # there should exist a unique '=' at the right-most position of the statement if ans.count('=') != 1: # not having one or having more than one # "=" not in right-most position terminate() if ans[-1] != '=': # has one but not at the right-most position # "=" not in right-most position terminate() # iterate ans for item in ans: # when item is an operator(excluding '=') if item in ['+', '-', '*', '/']: # detect stack underflow: need at least two operands for an operator.
# Program for Determining Palindromes import stack # welcome print ('This program can determine if a given string is a palindrome\n') print ('(Enter return to exit)') # init char_stack = stack.getStack() empty_string = '' # get string from user chars = input('Enter string to check: ') while chars != empty_string: if len(chars) == 1: print('A one letter word is by definition a palindrome\n') else: # init is_palindrome = True # determine half of length. excluding any middle character compare_length = len(chars) // 2 # push second half of input string on stack for k in range(compare_length, len(chars)): stack.push(char_stack, chars[k]) # pop chars and compare to first half of string k = 0
import stack import sys stk = stack.getStack() lst = list(input('Enter parentheses and/or braces: ')) for k in range(len(lst)): if lst[k] == '(': stack.push(stk, lst[k]) elif lst[k] == '{': stack.push(stk, lst[k]) elif lst[k] == ')': if stack.isEmpty(stk): print('Not properly nested.') sys.exit() if stack.pop(stk) != '(': print('Not properly nested.') sys.exit() elif lst[k] == '}': if stack.isEmpty(stk): print('Not properly nested.') sys.exit() if stack.pop(stk) != '{': print('Not properly nested.') sys.exit() if stack.isEmpty(stk): print('Nested properly.') else: print('Not properly nested.')
import stack mystack = stack.getStack() for item in range(1, 5): stack.push(mystack, item) print('Pushing', item, 'on stack') while not stack.isEmpty(mystack): item = stack.pop(mystack) print('Popping', item, 'from stack')
import stack """ Ask the user to enter a series of braces and parentheses, then indicate if they are properly nested """ char = input('Enter parentheses and/or braces: ') sequence = stack.getStack() #for every element in the received string checks the type and pushes/pops in/from the stack for el in char: if el == '(' or el == '{': stack.push(sequence, el) elif not stack.isEmpty(sequence): if el == ')' and stack.top(sequence) == '(': stack.pop(sequence) elif el == '}' and stack.top(sequence) == '{': stack.pop(sequence) #Final check if the stack is empty #If it's empty the string is proper if stack.isEmpty(sequence): print('Nested properly.') else: print('Not properly nested.')
# import stack module import stack # user input pa = input("Enter parentheses and/or braces: ") # define pa_stack pa_stack = stack.getStack() for i in range(0, len(pa)): # when character is left parentheses or braces push to the stack if pa[i] == "(" or pa[i] == "{": stack.push(pa_stack, pa[i]) # when character is right parentheses or braces pop the stack # if the value of the stack for top is left parentheses or braces else: if stack.top(pa_stack) == "(" and pa[i] == ")": stack.pop(pa_stack) elif stack.top(pa_stack) == "{" and pa[i] == "}": stack.pop(pa_stack) # output "Nested properly." when stack is empty if stack.isEmpty(pa_stack): print("Nested properly.") else: print("Not properly nested.")
result = multipl(a,b) stack.push(numbers, result) elif element == '/': result = division(a,b) stack.push(numbers, result) """ Main functionality of the program. Asks user to enter a sequence of operands and operators. Determines to which group it belongs and do the calculation. """ num = stack.getStack() while flag == True: exp = input('Enter an RPN expression: ') for el in exp: if el in operand: stack.push(num, el) elif el in operator: calculator(el, num) elif el == '=': print('Value of expression: ', int(stack.pop(num))) else: flag = False #Resets the stack num = stack.getStack()
operator = ('+', '-', '*', '/') inp = str(input('Enter an RPN expression: ')) # About if input means terminating this program while inp and not (inp not in '0123456789+-*/=' and len(inp) == 1): # If not oper for i in inp: if i not in '0123456789+-*/= ': termination() right_most_position = True oper_stack = stack.getStack() slot = inp.split() for item in slot: if not right_most_position: # print('right-most position') termination() if item in operator: operand_last = stack.pop(oper_stack) operand_second_last = stack.pop(oper_stack) if operand_second_last == None: # print('stack underflow') termination()
result = multipl(a, b) stack.push(numbers, result) elif element == '/': result = division(a, b) stack.push(numbers, result) """ Main functionality of the program. Asks user to enter a sequence of operands and operators. Determines to which group it belongs and do the calculation. """ num = stack.getStack() while flag == True: exp = input('Enter an RPN expression: ') for el in exp: if el in operand: stack.push(num, el) elif el in operator: calculator(el, num) elif el == '=': print('Value of expression: ', int(stack.pop(num))) else: flag = False #Resets the stack num = stack.getStack()