A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
7
Task/Letter-frequency/Python/letter-frequency-1.py
Normal file
7
Task/Letter-frequency/Python/letter-frequency-1.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import collections, sys
|
||||
|
||||
def filecharcount(openfile):
|
||||
return sorted(collections.Counter(c for l in openfile for c in l).items())
|
||||
|
||||
f = open(sys.argv[1])
|
||||
print(filecharcount(f))
|
||||
24
Task/Letter-frequency/Python/letter-frequency-2.py
Normal file
24
Task/Letter-frequency/Python/letter-frequency-2.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import string
|
||||
if hasattr(string, ''ascii_lowercase''):
|
||||
letters = string.ascii_lowercase # Python 2.2 and later
|
||||
else:
|
||||
letters = string.lowercase # Earlier versions
|
||||
offset = ord('a')
|
||||
|
||||
def countletters(file_handle):
|
||||
"""Traverse a file and compute the number of occurences of each letter
|
||||
"""return results as a simple 26 element list of integers.
|
||||
results = [0] * len(letters)
|
||||
for line in file_handle:
|
||||
for char in line:
|
||||
char = char.lower()
|
||||
if char in letters:
|
||||
results[offset - ord(char)] += 1
|
||||
# Ordinal of 'a' minus ordinal of any lowercase ASCII letter -> 0..25
|
||||
return results
|
||||
|
||||
if __name__ == "__main__":
|
||||
sourcedata = open(sys.argv[1])
|
||||
lettercounts = countletters(sourcedata)
|
||||
for i in xrange(len(lettercounts)):
|
||||
print "%s=%d" % (chr(i + ord('a')), lettercounts[i]),
|
||||
12
Task/Letter-frequency/Python/letter-frequency-3.py
Normal file
12
Task/Letter-frequency/Python/letter-frequency-3.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
...
|
||||
from collections import defaultdict
|
||||
def countletters(file_handle):
|
||||
"""Count occurences of letters and return a dictionary of them
|
||||
"""
|
||||
results = defaultdict(int)
|
||||
for line in file_handle:
|
||||
for char in line:
|
||||
if char.lower() in letters:
|
||||
c = char.lower()
|
||||
results[c] += 1
|
||||
return results
|
||||
3
Task/Letter-frequency/Python/letter-frequency-4.py
Normal file
3
Task/Letter-frequency/Python/letter-frequency-4.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
lettercounts = countletters(sourcedata)
|
||||
for letter,count in lettercounts.iteritems():
|
||||
print "%s=%s" % (letter, count),
|
||||
Loading…
Add table
Add a link
Reference in a new issue