示例#1
0
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)
示例#2
0
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)
示例#3
0
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
示例#4
0
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)
示例#5
0
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")
示例#6
0
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)
示例#7
0
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:
示例#8
0
import Module.Stack as st

kata = "(){}[]"
stack = st.stack()
pencarian = {')': '(', '}': '{', ']': '['}
# con = 0
# print(pencarian.keys())
# for cek in kata:
#     if cek == pencarian.keys():
#         st.push(stack, cek)
print(pencarian['}'])
示例#9
0
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
示例#10
0
def addList(x, y):
    cekX = st.size(x)
    cekY = st.size(y)
    hasil = st.stack()
    if cekX >= cekY:
        for i in range(cekX):
            if i < cekY:
                st.push(hasil, x[i] + y[i])
            else:
                st.push(hasil, x[i])
    else:
        for i in range(cekY):
            if i < cekX:
                st.push(hasil, x[i] + y[i])
            else:
                st.push(hasil, y[i])
    print(hasil)
示例#11
0
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")
示例#12
0
import Module.Stack as st
stack = st.stack()
decimal = 64
while decimal != 0:
    hasil = decimal % 2
    st.push(stack, hasil)
    decimal = decimal // 2
for i in range(len(stack)):
    print(st.pop(stack), end="")
示例#13
0
import Module.Stack as st
inpt = int(input("Masukkan Angka: "))
iterasi = 2
prima = st.stack()
bukan = st.stack()
while iterasi != inpt:
    for i in range(2, iterasi):
        if iterasi % i == 0:
            if iterasi % 2 == 1:
                st.push(bukan, iterasi)
            break
    else:
        st.push(prima, iterasi)
    iterasi = iterasi + 1
print("Bilangan Prima", prima)
print("Bilangan Bukan Prima", bukan)