コード例 #1
0
ファイル: Reverse_word.py プロジェクト: virgorasion/My-Python
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
ファイル: Postfix_sule.py プロジェクト: virgorasion/My-Python
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
コード例 #3
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)
コード例 #4
0
ファイル: Postfix.py プロジェクト: virgorasion/My-Python
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
ファイル: Postfix_sule.py プロジェクト: virgorasion/My-Python
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)
コード例 #6
0
ファイル: Postfix_sule.py プロジェクト: virgorasion/My-Python
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
コード例 #7
0
ファイル: Postfix.py プロジェクト: virgorasion/My-Python
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")
コード例 #8
0
ファイル: Postfix.py プロジェクト: virgorasion/My-Python
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)
コード例 #9
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:
コード例 #10
0
ファイル: cek_prima.py プロジェクト: virgorasion/My-Python
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)