def rev(s): st = [] top = 0 for i in s: top = m.push(st, i, top) #print('top =',top) rev = [] top1 = None for i in range(top + 1): top, e = m.pull(st, top) top1 = m.push(rev, e, top1) #print(''.join(rev)) #print('top1=',top1) return ''.join(rev)
'''3. Imagine that you are writing a program for a simple text editor. Using stacks how would you implement the undo and redo options? [consider a list as the text. Undo is the deletion of the latest character into the text and redo is pushing it back in].''' import mods as m st = [] u = [] topu = 0 top = 0 flag = 0 #print('welcome to text editor!( enter < and press enter to undo or enter > and press enter to redo OR just press enter to exit)') while True: n = input() for i in n: top = m.push(st, i, top) top, i = m.pull(st, top) #if i == '\n': #print('ahhhhhh') #top,e = m.pull(st,top) #topu = m.push(u,e,topu) #flag = 1 if i == '<': if top == None: print('cannot undo') else: top, e = m.pull(st, top) topu = m.push(u, e, topu) flag = 1 elif i == '>': if flag == 1: topu, e = m.pull(u, topu) top = m.push(st, e, top)
import mods as m st = [] top = 0 top = m.push(st,8,top) print(st) print(top) top = m.push(st,9,top) print(st) print(top) top,poop = m.pull(st,top) print(st) print(top) print('poop=',poop)
#2. Import the file into another program, send a string in the form of a stack. Reverse the string using pop. #3. Using the same file convert a decimal number into its binary equivalent using the stack functionality import mods as m #s = 'hellooo' def rev(s): st = [] top = 0 for i in s: top = m.push(st, i, top) #print('top =',top) rev = [] top1 = None for i in range(top + 1): top, e = m.pull(st, top) top1 = m.push(rev, e, top1) #print(''.join(rev)) #print('top1=',top1) return ''.join(rev) st1 = [] top2 = 0 n = int(input('enter number')) while n >= 1: top2 = m.push(st1, str(n % 2), top2) n = n // 2 print(st1) print(rev(''.join(st1)))
d = {"0": 0, "/": 4, "*": 3, "+": 2, "-": 1} l1 = [] top = len(l1) print(top) for a in l: if a not in ( "-", "+", "*", "/", "(", ")" ): #(ord(a)>=65 and ord(a)<=90) or (ord(a)>=97 and ord(a)<=122)or(a=="1" or "2" or"3"or"4"or"5"or"6"or"7"or"8"or"9"): print(a, end="") if a in ("-", "+", "*", "/"): print('t', top) while (top != 0) and (d[a] <= d[l1[top - 1]]) and (l1[top - 1] != "("): p = stacks.pull(l1, top) top = len(l1) print(p[1], end="") m.push(l1, a, top) top = len(l1) if a == "(": m.push(l1, a, top) top = len(l1) if a == ")": while l1[top] != "(": m = m.pull(l1) print(m[1], end="") l = m.pull(l1) print(top) while top != 0: x = stacks.pop(l1) print(x[1])