1.11M Members

Counting occurance of letters in a string

 
0
 

I have to write a program that will decode a Ceasar cypher with any possible rotation length on a long string that will be imported into the program. I'm trying to figure out some pseudo coding on how I want to tackle this problem but there is one key thing that I cannot figure out.

I have no clue how to go about writing a function that will find the largest occurrence of a letter in a string, and use that percentage of occurrence to find the amount shifted from a standard percentage of letter usage in the English language.

Essentially what I want to do is find the most used letter in the string, then find the difference between the encoded string max used letter and a constant given, to find my rotation key. Then use that key to decode the string.

Any help with ideas of possible ways to create a function that will count the occurrence of letters in a string will be big step forward as I go on writing the rest of my program.

 
0
 

Let me help you to find your first big step forward: type "python count letters in string" in google and click on the first link.

 
0
 

I have but most methods use a dictionary or complicated method which I haven't learned yet still being new to python. So I was hoping to talk about other methods to do it instead of having someone point me to something that I obviously would already have done. But thanks for the help anyway

 
0
 

This may give you some ideas:

>>> 'abcdefg'.count('a')
1
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> s = 'I have to write a program that will decode a Ceasar cypher with any possible rotation length'
>>> dd = dict(zip(string.ascii_lowercase, [s.count(c) for c in string.ascii_lowercase]))
>>> dd['e']
8
>>>
 
0
 

I can get a function to find the largest number of times any letter is used, but what I don't know is how to let python use the actual letter itself that is used the most. Such as if 'T' happens to be used the most then use the ascii value of ord(T) for the rest of my program instead of simply finding an int value telling me the most times anything is used.

 
0
 

I'm not sure i quite get what your trying to do?
Wouldn't "T" be the key to your dict, and the value is the total count as you say?

 
0
 

You just do undecoding with -most frequent code + code for most common letter to do opposite shift.

Question Answered as of 2 Years Ago by NewbieXcellence, bvdet and pyTony
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: