[Tutor] Eng to Leet Speek
Dave Angel
davea at ieee.org
Mon Jul 27 21:08:08 CEST 2009
Chris Castillo wrote:
> so I have a string:
>
> 1 4|-| 50 |_33+. I love [#ick3n 4nd ch3353. Don't you love +|_|2|\e7
> \/\/1+# the |#a|-|i|_7?
>
>
> and I am trying to turn it into english with the following code:
>
> fileIn = open("encrypted.txt", "r").read()
>
> def eng2leet(astring):
> astring = astring.replace("4","a")
> astring = astring.replace("8","b")
> astring = astring.replace("[","c")
> astring = astring.replace("|)","d")
> astring = astring.replace("3","e")
> astring = astring.replace("|#","f")
> astring = astring.replace("6","g")
> astring = astring.replace("#","h")
> astring = astring.replace("1","i")
> astring = astring.replace("]","j")
> astring = astring.replace("|\\","k")
> astring = astring.replace("|_","l")
> astring = astring.replace("|-|","m")
> astring = astring.replace("|\\","n")
> astring = astring.replace("0","o")
> astring = astring.replace("|*","p")
> astring = astring.replace("0\\","q")
> astring = astring.replace("2","r")
> astring = astring.replace("5","s")
> astring = astring.replace("+","t")
> astring = astring.replace("|_|","u")
> astring = astring.replace("\/","v")
> astring = astring.replace("\/\/","w")
> astring = astring.replace("><","x")
> astring = astring.replace("7","y")
> astring = astring.replace("7_","z")
> return astring
>
> print eng2leet(fileIn)
>
> Only problem is that when it needs to translate a U or a W it prints an L or
> 2 V's. Need some help please. Thanks
>
>
Your problem is in the order of substitution. If you put the "v" test
*after* the "w" test, you'll avoid one of your problems. And put the
"l" test after the "u" test. And you didn't mention it, but "z" should
come before "y". This is because some of your strings are substrings of
others. A more general rule might be to put all the four-character
substitutions first, then do all the three-character ones, then two,
then one. That's not guaranteed to work, but by inspection I think it will.
Another problem that could have hit you is that "\" is an escape
character in strings. So you're taking advantage of the fact that \/
(for example) doesn't happen to be a valid escape sequence. The usual
workaround is to use raw strings. For another example, look at the
string for "n". Did you want two backslashes? You'll only get one as
it sits.
Did you mean for "k" and "n" to be the same?
DaveA
More information about the Tutor
mailing list