By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

How to write a program to decode a message?

P: 23
My program needs to fix an encrypted text file. The person who wrote it put it in “leet speak”, with special characters representing various letters. For example, consider the sample below:
1 4|-| 50 |_33+.
is really
I am so leet.
Here is the conversion table I need to use.
A 4
B 8
C [
D |)
E 3
F |#
G 6
H #
I 1
J ]
K |\
L |_
M |-|
N |\|
O 0
P |*
Q 0\
R 2
S 5
T +
U |_|
V \/
W \/\/
X ><
Y 7
Z 7_
Your program should read leet speak sentences from a file called "leet.txt" (one sentence per line). It should covert each sentence to normal English and print it to the screen.
here is what I have:
Expand|Select|Wrap|Line Numbers
  1. d= {"4":"A", "8":"B", "[":"C", "|)":"D", "3":"E", "|#":"F", "6":"G", "#":"H", "1":"I", "]":"J", "|\\":"K", "|_":"L", "|-|":"M", "|\\|":"N", "0":"O", "|*":"P", "0\\":"Q", "2":"R", "5":"S", "+":"T", "|_|":"U", "\\/":"V", "\\/\\/":"W", "><":"X", "7":"Y", "7_":"Z"}
  2. infile = open("leet.txt", "r")
  3. for line in infile:
  4.     line.split
  5.     window = 4
  6.     i = 0
  7.     while i < len(line):
  8.         t = line[i:i+window]
  9.         i += window
  10.     print (line)
  11.  
It is printing in leet but where do I put in the line to map it to the dictionary? And How do I move down?
Nov 29 '10 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 624
You look up each character in the dictionary http://www.java2s.com/Code/Python/Di...lternative.htm http://books.google.com/books?id=1Hx...ership&f=false. Of course, the character "|" and others signal a multiple character conversion, so for those characters you would have to test for 2 characters, then 3, etc. until a match is found.
Expand|Select|Wrap|Line Numbers
  1. d= {"4":"A", "8":"B", "[":"C", "|)":"D", "3":"E", "|#":"F", "6":"G", "#":"H", "1":"I", "]":"J", "|\\":"K", "|_":"L", "|-|":"M", "|\\|":"N", "0":"O", "|*":"P", "0\\":"Q", "2":"R", "5":"S", "+":"T", "|_|":"U", "\\/":"V", "\\/\\/":"W", "><":"X", "7":"Y", "7_":"Z"}
  2.  
  3. ch ='4'
  4. print "%s translates to %s" % (ch, d[ch])
  5. ch =']'
  6. print "%s translates to %s" % (ch, d[ch]) 
Nov 29 '10 #2

P: 23
We are supposed to start with 4 which is the longest character and work back to 1.
Nov 29 '10 #3

Expert 100+
P: 624
You will have to start with 4 characters and remove the right-most under a while() loop until it is found. Ask you instructor what happens if you have "O" followed by "V" or "W". How can you tell that it is not a "Q":
"OV" = 0\/ ## will find "Q" = "0\" going from 4 chars to one
"OW" = 0\/\/ ## will find "Q" = "0\"
"Q" = 0\
"O" = 0
Unless you copied the dictionary wrong. Changing "Q" to a forward slash, 0/ would fix it.
Nov 30 '10 #4

Expert 100+
P: 624
A very quick and dirty solution that hopefully will help you solve this for yourself. It does not allow for a malformed leet that is not found in the lookup dictionary, and probably other things as well, but that is up to you to do.
Expand|Select|Wrap|Line Numbers
  1. def from_leet(d, phrase, ctr):
  2.     to_add = 4
  3.     if ctr+to_add >= len(phrase):
  4.         to_add = len(phrase) - ctr    
  5.  
  6.     for x in range(to_add, 0, -1):
  7.         test_this = phrase_leet[ctr:ctr+x]
  8.         if test_this in d:
  9.             return(ctr+x, d[test_this])
  10.     return ctr, ""
  11.  
  12. def to_leet(d, phrase_in):
  13.     d_reverse = {}
  14.     for key in d:
  15.         d_reverse[d[key]] = key
  16.     phrase_in = phrase_in.upper()
  17.     output_list = []
  18.     for ch in phrase_in:
  19.         output_list.append(d_reverse[ch])
  20.     return("".join(output_list))
  21.  
  22.  
  23. d= {"4":"A", "8":"B", "[":"C", "|)":"D", "3":"E", "|#":"F", "6":"G", 
  24.     "#":"H", "1":"I", "]":"J", "|\\":"K", "|_":"L", "|-|":"M", "|\\|":"N", 
  25.     "0":"O", "|*":"P", "0\\":"Q", "2":"R", "5":"S", "+":"T", "|_|":"U", 
  26.     "\\/":"V", "\\/\\/":"W", "><":"X", "7":"Y", "7_":"Z"}
  27.  
  28. ##phrase = "quickbrownfox"     ## "ow" problem
  29. phrase = "whydoesthecagedbirdsing"
  30. phrase_leet = to_leet(d, phrase)
  31. print phrase_leet, "\n"
  32.  
  33. ctr = 0
  34. output_list = []
  35. while ctr < len(phrase_leet):
  36.     ctr, letter = from_leet(d, phrase_leet, ctr)
  37.     output_list.append(letter)
  38.  
  39. print "".join(output_list) 
Nov 30 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.