Search code examples
pythonradix

Change from base Shadocks to Base 10 in Python


I have a little problem who block me, I've a work where I must to convert a number to Shadocks (base 4 it seems), and I must to make a decrypter. So I made the first part, but my code won't work on the second. Here it's :

def Base10toShadocks(n):
    q = n
    r = 0
    Base4=[]
    Shads=["GA","BU","ZO","MEU"]
    if q == 0:
        Base4.append(0)
    else:
         while q > 0:
             q = n//4
             r = n%4
             n = q
             Base4.append(r)
         Base4.reverse()
         VocShad = [Shads[i] for i in Base4]
    print(VocShad)
def ShadockstoBase10(n):
    l=len(n)
    Erc_finale=[]
    for i in range(l):
        Sh=(n[i])
        i=i+1
        if Sh =="a":
            Erc_finale.append(0)
        elif Sh =="b":
            Erc_finale.append(1)
        elif Sh =="o":
            Erc_finale.append(2)
        elif Sh =="e":
            Erc_finale.append(3)
     print(Erc_finale)
     F=str(Erc_finale)
     print(F)
     F=F.replace("[","")
     F=F.replace("]","")
     F=F.replace(",","")
     F=F.replace(" ","")
     L2=len(F)
     F=int(F)
     print(L2)
     print(F)
     r=0
     while f < 4 or F ==4:
        d=(F%4)-1
        F=F//4
        print(d)
        r=r+d*(4**i)
        print(r)

inp = 0
inp2 = 0
print("Tapez \"1\" pour choisir de traduire votre nombre en shadock, ou \"2\" pour inversement")
inp = int(input())
if inp == 1:
    print("Quel est le nombre ?")
    inp2 = int(input())
    if inp2 != None:
        Base10toShadocks(inp2)
elif inp == 2:
    print("Quel est le nombre ?")
    inp2 = str(input())
    if inp2 != None:
        ShadockstoBase10(inp2)

It blocks at the F=int(F), I don't understand why. Thanks for your help.


Solution

  • First, some errors in your code:

    for i in range(l):
        Sh=(n[i])
        i=i+1      #### Won't work. range() will override
        ##### Where do "a","b","o","e" come from
        ##### Shouldn't it be "G","B","Z","M" ("GA","BU","ZO","MEU")?
        if Sh =="a":
            Erc_finale.append(0)
        elif Sh =="b":
            Erc_finale.append(1)
        elif Sh =="o":
            Erc_finale.append(2)
        elif Sh =="e":
            Erc_finale.append(3)
     print(Erc_finale)
     F=str(Erc_finale)    ### Not how you join an array into a string
    

    Here's a corrected way:

    def ShadockstoBase10(n):
        n = n.upper();    # Convert string to upper case
        l = len(n)
        Erc_finale = ""   # Using a string instead of an array to avoid conversion later
        i = 0
        while i < l:      # while loop so we can modify i in the loop
            Sh = n[i:i+2] # Get next 2 chars
            i += 2        # Skip 2nd char
            if Sh == "GA":
                Erc_finale += "0"
            elif Sh == "BU":
                Erc_finale += "1"
            elif Sh == "ZO":
                Erc_finale += "2"
            elif Sh =="ME" and "U" == n[i]:
                Erc_finale += "3"
                i += 1;      # MEU is 3 chars
            else:
                break;       # bad char
        return int(Erc_finale, 4)  # Let Python do the heavy work
    

    Like everything in Python, there are other ways to do this. I just tried to keep my code similar to yours.