Home > front end >  How can I show all the complete message of the algorithm?
How can I show all the complete message of the algorithm?

Time:08-10

I’m trying to create a binary tree about the Morse Code. The program should show all the message but for some reason it only prints the first letter. What should I do?

class Tree: 

    def __init__(self, data):
        self.data = data
        self.izq = None
        self.der = None

Arriba = Tree ("    ")

Arriba.izq = Tree("E")

Arriba.der = Tree("T")

Arriba.izq.izq = Tree("I")

Arriba.izq.der = Tree("A")

Arriba.der.izq = Tree("N")

Arriba.der.der = Tree("M")

Arriba.izq.izq.izq = Tree("S")

Arriba.izq.izq.der = Tree("U")

Arriba.izq.der.izq = Tree("R")

Arriba.izq.der.der = Tree("W")

Arriba.der.izq.izq = Tree("D")

Arriba.der.izq.der = Tree("K")

Arriba.der.der.izq = Tree("G")

Arriba.der.der.der = Tree("O")

Arriba.izq.izq.izq.izq = Tree("H")

Arriba.izq.izq.izq.der= Tree("V")

Arriba.izq.izq.der.izq = Tree("F")

Arriba.izq.der.izq.izq = Tree("L")

Arriba.izq.der.der.izq = Tree("P")

Arriba.izq.der.der.der = Tree("J")

Arriba.der.izq.izq.izq = Tree("B")

Arriba.der.izq.izq.der = Tree("X")

Arriba.der.izq.der.izq = Tree("C")

Arriba.der.izq.der.der = Tree("Y")

Arriba.der.der.izq.izq = Tree("Z")

Arriba.der.der.izq.der = Tree("Q")

texto = input("Digite el mensaje cifrado: ")

index = 0
def Cositas (Arriba):
    global index
  
    for i in texto[index]:
        lista  = []
    
        if i == ".":
            index  = 1
            Cositas (Arriba.izq)
    
        elif i == "-":
            index  =1
            Cositas (Arriba.der)

        elif i == " ":
            index  = 1
            lista.append(Arriba.data)

        elif i == "/" :
            index  = 1
            lista.append("---")
      

    for j in lista:
        print("Su mensaje es:")
        print(j,end="")

Cositas(Arriba)

CodePudding user response:

These are some of the issues:

  • When i is a space, the process stops: no more calls to Cositas are made, and the letters in texto that come after that space are not processed.

  • When / is encountered, a similar problem happens. No more progress is made.

  • Every recursive call would print out "Su mensaje es:". It would be more appropriate to not use recursion here, but use loops only

  • The for i in texto[index] statement will only iterate once, because texto[index] is just one character.

  • It is bad practice to use a global index variable. If you would need to decode multiple messages, you would need to reset it to 0 again. This is not elegant.

I would suggest doing this without recursion. So after you have built the tree Arriba, you could continue like this:

def cositas (arriba, texto):
    words = []
    for word in texto.split("/"):
        lista = []
        for code in word.split():
            node = arriba
            for ch in code:
                node = node.izq if ch == "." else node.der
            lista.append(node.data)
        words.append("".join(lista))
    return " ".join(words)
        
texto = input("Digite el mensaje cifrado: ")
translation = cositas(Arriba, texto)
print("Su mensaje es:", translation)

So if for instance you would input:

-.-. --- -- . / --.- ..- .. -.-. -.- / . -. --. .. -. . / .-. --- --- -- / -. . .- .-. .-.. -.-- / ..-. ..- .-.. .-..

...the output would be:

Su mensaje es: COME QUICK ENGINE ROOM NEARLY FULL

  • Related