Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
Replacing a letter with "#" and reverse

I am new to python and doing a test program that replaces all the letters in a given text with signs.

def encrypted(request):
    text = request.GET['text']
    
    text = text.replace("a", "#.")
    text = text.replace("b", "##.")
    text = text.replace("c", "###.")
    text = text.replace("d", "####.")
    text = text.replace("e", "#####.")
    text = text.replace("f", "######.")
    etc...

return render(request, 'encrypted.html', {'text': text})

I have achieved that but I have tried reversing it the same way and it does not work.

def decrypted(request):
    ftext = request.GET['text']

    ftext = ftext.replace("#.", "a")
    ftext = ftext.replace("##.", "b")
    ftext = ftext.replace("###.", "c")
    ftext = ftext.replace("####.", "d")
    ftext = ftext.replace("#####.", "e")
    etc...

    return render(request, 'decrypted.html')

So the text does not appear at all on the page.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>yCrypt</title>
</head>
<body>
TEXT DECRYPTION: {{ftext}}
</body>
</html>

I wonder why it shows no code issues whatsoever. Maybe there is a simplier way to make this possible?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I'm not entirely following what you're trying to do here, but your first replace call in the decrypted function is going to find and replace every occurrence of "#." with the letter "a" in every "encrypted letter", not just the one for "a".

The subsequent calls to replace won't match anything because their trailing "#." has already been erroneously replaced with the letter "a".

For this to work as designed you'd have to reverse the order of your replace calls such that you're finding the longest matches first.

And for it to be rendered you also have to pass it in the context to your template renderer.

def decrypted(request):
    ftext = request.GET['text']

    etc...
    ftext = ftext.replace("#####.", "e")
    ftext = ftext.replace("####.", "d")
    ftext = ftext.replace("###.", "c")
    ftext = ftext.replace("##.", "b")
    ftext = ftext.replace("#.", "a")

    return render(request, 'decrypted.html', { "ftext": ftext }) 
over 4 years ago · Santiago Trujillo Relatório

0

Not an expert but you can try

def decrypted(request):
    ftext = request.GET['text']

    ftext = ftext.replace("#.", "a")
    ftext = ftext.replace("##.", "b")
    ftext = ftext.replace("###.", "c")
    ftext = ftext.replace("####.", "d")
    ftext = ftext.replace("#####.", "e")
    return render(request, 'decrypted.html',{'ftext':ftext})
over 4 years ago · Santiago Trujillo Relatório

0

The reason it is not working is that encoded of a is a sub-string of b, c, d, e, etc.

a = #.
b = ##.
c = ###.

as you can see #. is at the end of every encoded string

So when you first encode, let's say abc it becomes #.##.###. When you run the decode step you use replace("#.", "a") you get the result a#a##a where every #. is replaced by a and as you can see there are no dots left.

The solution is quite simple instead of ###. format, use .###. format

a = .#.
b = .##.
c = .###.

this will ensure only the correct number of # matches and not a substring.

So when you first encode, let's say abc it becomes .#..##..###. When you run the decode step you use replace(".#.", "a") you get the result a.##..###. as you can see codes for b and c are still there.

On a side note, you can use loops to make code much smaller.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda