How to write a program to decode a message?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anderson1373
    New Member
    • Nov 2009
    • 23

    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:
    Code:
    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"}
    infile = open("leet.txt", "r")
    for line in infile:
    	line.split
    	window = 4
    	i = 0
    	while i < len(line):
    		t = line[i:i+window]
    		i += window
    	print (line)
    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?
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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.
    Code:
    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"}
    
    ch ='4'
    print "%s translates to %s" % (ch, d[ch])
    ch =']'
    print "%s translates to %s" % (ch, d[ch])

    Comment

    • anderson1373
      New Member
      • Nov 2009
      • 23

      #3
      We are supposed to start with 4 which is the longest character and work back to 1.

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        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.

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          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.
          Code:
          def from_leet(d, phrase, ctr):
              to_add = 4
              if ctr+to_add >= len(phrase):
                  to_add = len(phrase) - ctr    
          
              for x in range(to_add, 0, -1):
                  test_this = phrase_leet[ctr:ctr+x]
                  if test_this in d:
                      return(ctr+x, d[test_this])
              return ctr, ""
          
          def to_leet(d, phrase_in):
              d_reverse = {}
              for key in d:
                  d_reverse[d[key]] = key
              phrase_in = phrase_in.upper()
              output_list = []
              for ch in phrase_in:
                  output_list.append(d_reverse[ch])
              return("".join(output_list))
          
          
          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"}
          
          ##phrase = "quickbrownfox"     ## "ow" problem
          phrase = "whydoesthecagedbirdsing"
          phrase_leet = to_leet(d, phrase)
          print phrase_leet, "\n"
          
          ctr = 0
          output_list = []
          while ctr < len(phrase_leet):
              ctr, letter = from_leet(d, phrase_leet, ctr)
              output_list.append(letter)
          
          print "".join(output_list)

          Comment

          Working...