473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to write a program to decode a message?

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
4 4347
dwblas
626 Expert 512MB
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
We are supposed to start with 4 which is the longest character and work back to 1.
Nov 29 '10 #3
dwblas
626 Expert 512MB
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
dwblas
626 Expert 512MB
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

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

Similar topics

3
by: Simon Wigzell | last post by:
I recently wrote a program with MS Visual Studio C++, sent it off to the client where it didn't run, after some probing I discover they are on a Mac! My program is a MSF interface that is really...
3
by: simon lee | last post by:
Dear all, I use VC++ to write a program for display a flash file "file.fla", However, for displaying flash file, it sometimes become black screen and sometimes cannot display the corresponding...
22
by: Saurabh Saxena | last post by:
can we write the program to write no 1 to n without using switch,do,while,for,goto,if and conditional operator where n will be input by user.
0
by: Tom | last post by:
I am new to hardware programming. I need to write a program for reading data from Card Reader which connects to the PC windows 2000/XP OS through Interfacing The Serial / RS-232 Port / USB /...
2
by: shaveta | last post by:
The problem is like this:- A maze is a rectangular area, with m rows and n columns, with an entrance and an exit. The interior of the maze contains obstacles. The entrance is at the upper-left...
1
by: shilkigupta | last post by:
plz write a program to print 4 4 3 4 4 3 2 3 4 4 3 2 1 2 3 4 4 3 2 3 4 4 3 4 ...
7
by: ASIM ABBAS | last post by:
sri my questuon is that write a program in corc++ to check wether a relation is equlance or not using two dimensional array
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.