Last active
January 10, 2016 20:57
-
-
Save chazcheadle/005c8c9639a8c3dc793d to your computer and use it in GitHub Desktop.
Get frequency of letters in a given body of text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/python | |
| text = \ | |
| '''Sing in me, Muse, and through me tell the story | |
| of that man skilled in all ways of contending, | |
| the wanderer, harried for years on end, | |
| after he plundered the stronghold | |
| on the proud height of Troy. | |
| He saw the townlands | |
| and learned the minds of many distant men, | |
| and weathered many bitter nights and days | |
| in his deep heart at sea, while he fought only | |
| to save his life, to bring his shipmates home.''' | |
| frequency = {'space':0,'punct':0,'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0} | |
| def getFrequency(text): | |
| text = text.lower() | |
| for l in text: | |
| if l == ' ': | |
| l = 'space' | |
| if not l.isalpha(): | |
| l = 'punct' | |
| frequency[l] += 1 | |
| for letter in sorted(frequency): | |
| if letter is not 'space' and letter is not 'punct': | |
| print(letter, frequency[letter]) | |
| def main(): | |
| getFrequency(text) | |
| if __name__=='__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment