Decoder program

Encoded with caesar cipher, need make generic decoder

Page 1 of 1

12 Replies - 5010 Views - Last Post: 28 February 2010 - 12:41 PM Rate Topic: -----

#1 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Decoder program

Posted 26 February 2010 - 04:03 PM

Greetings,

Ok so what I am trying to do for a homework is make a generic decoder on any encrypted text ( file.txt) that have been encrypted with caesar cipher. We found that 'E' was the most frequently used letter in a given example text. In this case for my homework, my teacher gave me the preamble to the constitution encrypted. And I have to somehow find the most reoccurring letter in the encrypted text. Then use that letter to find the shift amount. From there the program has to decode based on the shift amount.

My problem right now is finding out how to get the most frequently used letter.
I was thinking of putting the txt through a loop, but I have absolutely no Idea what to do next..
Maybe sort then find w/e "string" is largest or most occurred.. I dunno, im really stumped. Can anyone point me in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Decoder program

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Decoder program

Posted 26 February 2010 - 04:18 PM

Should probably tell us whether this is Perl or Python.
Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Decoder program

Posted 26 February 2010 - 04:24 PM

Look at section 5.8 and consider setting up a dictionary for every letter in the alphabet:
http://docs.python.o...y/stdtypes.html

You gotta show me some code before I show you code.

This post has been edited by atraub: 26 February 2010 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

#4 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Re: Decoder program

Posted 26 February 2010 - 05:13 PM

Ah my apologies, forgot this was python and perl.
In regards to my situation the code is python
And I will get right on working on some codes, ill send something in soon
Was This Post Helpful? 0
  • +
  • -

#5 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Decoder program

Posted 26 February 2010 - 05:57 PM

stuff = "i like pie"
letters = {}
for c in stuff:
     if not letters.has_key(c):
        letters[c] = 1
     else:
        letters[c] += 1
print letters['i'] # should print 3
print letters['e'] # should print 2



Should get you started.
Was This Post Helpful? 0
  • +
  • -

#6 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Re: Decoder program

Posted 27 February 2010 - 02:20 PM

Im sorry I couldn't get very far T.T i seem to have hit a roadblock.

What I am trying to do is use redirection and use any given textfile to be put into my decoder program.
My problem is I don't know how to make the file input into a variable in my program.
python hw3.py < code.txt

I have something like filename = raw_input()
print filename

to test, but I get a EOFError
Was This Post Helpful? 0
  • +
  • -

#7 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Re: Decoder program

Posted 27 February 2010 - 02:52 PM

-made too many changes gonna remove this post and put new one in later-

This post has been edited by cnampheonix: 27 February 2010 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Re: Decoder program

Posted 27 February 2010 - 03:26 PM

Im having trouble getting the shift amount.
I know the most reoccurring letter is 'e' with about a 12.701% frequency
so what I tried to do was get the frequency of each letter and if it was greater than or equal to 11% then it would take that element # and subtract 4 to get the shift amount.


for char in uppers:
    if char.count / 100 >= .11:
        ROT_AMOUNT = uppers[char] - 4


This post has been edited by cnampheonix: 27 February 2010 - 07:08 PM

Was This Post Helpful? 0
  • +
  • -

#9 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Decoder program

Posted 27 February 2010 - 09:49 PM

I think you might need
if char.count / 100.0 >= 0.11:


Instead.
Was This Post Helpful? 0
  • +
  • -

#10 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Re: Decoder program

Posted 27 February 2010 - 10:20 PM

I tried it however, its saying if ROT_AMOUNT < 0 is undefined,
I tried putting a return ROT_AMOUNT, but it keeps saying return is outside the function
I tried placing the return ROT_AMOUNT everywhere
-removed for plagiarism concerns

This post has been edited by cnampheonix: 28 February 2010 - 09:39 AM

Was This Post Helpful? 0
  • +
  • -

#11 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Re: Decoder program

Posted 28 February 2010 - 09:38 AM

I was able to get everything working however when I get the decoded message everything comes out with spaces inbetween all letters and spaces. C A T I N T H E H A T - example
Is there a way to fix this?
Also my code gets cut off the end of the line and writes the rest of the word on the next line. Is there a way to fix it so it won't cut words when they reach the end of the line?

-removed for plagiarism concerns-

This post has been edited by cnampheonix: 28 February 2010 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#12 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Decoder program

Posted 28 February 2010 - 09:58 AM

What is happening is that print always prints a space between data. To solve this, you can add each character to a final string and then only output that string at the end of the loop like this:
outstr = ""
for char in whatever:
    outstr += char
print outstr


Was This Post Helpful? 1
  • +
  • -

#13 cnampheonix  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 03-December 09

Re: Decoder program

Posted 28 February 2010 - 12:41 PM

Thanks a million programble, I had a hard time searching around trying to find out how to get rid of the spaces. My program works great now, thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1