Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda