def in2Post(strinfix): operator = {"+": 1, "-": 2, "*": 3, ":": 4} kurungg = {")": "(", "}": "{", "]": "["} dataa = st.stack() output = "" bagi = [] temp = "" for j in strinfix: if j == " ": continue elif j not in operator.keys() and j not in kurungg.keys( ) and j not in kurungg.values(): temp += j else: if temp != "": bagi.append(temp) temp = "" bagi.append(j) if temp != "": bagi.append(temp) for y in bagi: if y == "": continue elif y in kurungg.values(): st.push(dataa, y) elif y in operator.keys(): if st.size(dataa) != 0: p = st.peek(dataa) if p not in operator.keys(): st.push(dataa, y) elif p in operator.keys(): if operator[y] > operator[a]: st.push(dataa, y) elif operator[y] < operator[a]: output += st.pop(dataa) st.push(dataa, y) else: st.push(dataa, y) elif y in kurungg.keys(): while not st.isEmpty(dataa): q = st.pop(dataa) if q in operator.keys(): output += " " + q else: output += " " + y if st.size(dataa) > 0: while not st.isEmpty(dataa): output += " " + st.pop(dataa) return output
def evaluatePost(strPostfix): stack = st.stack() data = strPostfix.split() for i in data: # check is i a number if i.isdigit(): # then push to stack st.push(stack, i) # is not a number then ... else: # change : to / to eval the number if i == ":": i = "/" op1 = st.pop(stack) op2 = st.pop(stack) # to calculate the number then convert to str and push to stack st.push(stack, str(eval(op2 + i + op1))) return(stack)
def evaluatePost(strinfix): operatorr = "*:+-" dataaa = st.stack() bagii = strinfix.split() for k in bagii: if k not in operatorr: st.push(dataaa, int(k)) elif st.size(dataaa) > 1: r = st.pop(dataaa) t = st.pop(dataaa) if k == ":": k = "/" hasil = eval(" {}{}{} ".format(float(t), k, float(r))) st.push(dataaa, float(hasil)) return st.pop(dataaa)
def reverse(kata): a = st.stack() n = len(kata) for i in range(0, n, 1): st.push(a, kata[i]) hasil = "" for i in range(0, n, 1): hasil += st.pop(a) print(hasil)
def isValidate(strinfix): data = st.stack() kurung = {")": "(", "}": "{", "]": "["} for i in strinfix: if i in kurung.values(): st.push(data, i) elif i in kurung.keys(): if st.isEmpty(data): return "Kelebihan kurung tutup" else: a = st.peek(data) if a == kurung[i]: st.pop(data) else: return "Kurung tutup tidak sama" if st.size(data) > 0: print("Kelebihan kurung buka") return False else: return True
def isValidate(strInfix): stack = st.stack() con = 0 for cek in strInfix: if cek in kurung.values(): st.push(stack, cek) elif cek in kurung.keys(): if st.isEmpty(stack): print("Kelebihan Kurung Tutup") con = 1 break elif not st.dataCheck(kurung,st.peek(stack),cek): st.pop(stack) else: print(st.peek(stack), cek, "Kesalahan Kurung Tutup") con = 1 break if st.isEmpty(stack) and con == 0: return True elif not st.isEmpty(stack) and con == 0: print("Kelebihan Kurung Buka")
def in2Post(strInfix): stack = st.stack() result = "" # Loop each strInfix for i in strInfix: # to skip iteration if found space if i == " ": continue # check if i is a number elif i.isdigit(): result += i # check if i is kurung buka elif i in kurung.values(): st.push(stack,i) # check if i operator elif i in operator.keys(): # to separate each number result += " " # loop if stack is not empty and check level of operator while not st.isEmpty(stack) and st.compareOperator(operator, stack, i): # pop operator if current operator more higher level result += st.pop(stack)+" " # to push current operator after poping lower level operator st.push(stack,i) # to catch kurung tutup else: # loop if stack is not empty and check if kurung buka == kurung tutup while not st.isEmpty(stack) and st.dataCheck(kurung,st.peek(stack), i): # poping everything in the kurung :v result += " "+st.pop(stack) # last, pop kurung buka st.pop(stack) # check if stack is not empty while not st.isEmpty(stack): # pop everything remaining from the stack result += " "+st.pop(stack) return(result)
import Module.Stack as st stack = st.stack() print( "Pilihan Menu:\n1. Decimal to Biner\n2. Decimal to Octal\n3. Decimal to Hexa\n4. Biner to Decimal\n5. Octal to Decimal\n6. Hexa to Decimal" ) pilihan = int(input("Masukkan Kode Pilihan: ")) if pilihan == 1: decimal = int(input("Masukkan Bilangan Decimal: ")) while decimal != 0: hasil = decimal % 2 st.push(stack, hasil) decimal = decimal // 2 for i in range(len(stack)): print(st.pop(stack), end="") elif pilihan == 2: octal = int(input("Masukkan Bilangan Decimal: ")) while octal > 0: hasil = octal % 8 st.push(stack, hasil) octal = octal // 8 for i in range(st.size(stack)): print(st.pop(stack), end="") elif pilihan == 3: hexa = int(input("Masukkan Bilangan Decimal: ")) while hexa > 0: hasil = hexa % 16 if hasil == 10: hasil = "A" elif hasil == 11:
import Module.Stack as st kata = "{[23+56]*[12-2]:[10+20]]" stack = st.stack() con = 0 for cek in kata: if cek == "(" or cek == "{" or cek == "[": st.push(stack, cek) elif cek == ")" or cek == "}" or cek == "]": if st.isEmpty(stack): print("Kelebihan Kurung Tutup") con = 1 break elif st.peek(stack) == "(" and cek == ")" or st.peek( stack) == "{" and cek == "}" or st.peek( stack) == "[" and cek == "]": st.pop(stack) else: print(st.peek(stack), cek, "Kesalahan Kurung Tutup") con = 1 break if st.isEmpty(stack) and con == 0: print("Ntaps, Wes Bener") elif not st.isEmpty(stack) and con == 0: print("Kelebihan Kurung Buka")