Solution to Assignment 3 - Count letters (Due: Wednesday, October 17, 2018)
macbook-pro:hw3 raj$ more h.py
import string
import sys
def getCounts(fname):
with open(fname, "rb") as f:
data = f.read().decode("UTF-8")
s = data.lower()
answer = {}
for x in string.ascii_lowercase:
answer[x] = s.count(x)
return answer
def main():
counts = getCounts(sys.argv[1])
for k in sorted(counts):
print(k, counts[k])
main()
Here are sample runs against the two data files:
macbook-pro:hw3 raj$ python h.py hob
('a', 280)
('b', 68)
('c', 140)
('d', 98)
('e', 517)
('f', 86)
('g', 63)
('h', 163)
('i', 310)
('j', 10)
('k', 15)
('l', 143)
('m', 60)
('n', 338)
('o', 294)
('p', 104)
('q', 3)
('r', 227)
('s', 332)
('t', 312)
('u', 152)
('v', 19)
('w', 88)
('x', 25)
('y', 71)
('z', 0)
macbook-pro:hw3 raj$ python h.py lipogram
('a', 1466)
('b', 242)
('c', 427)
('d', 508)
('e', 1)
('f', 356)
('g', 459)
('h', 520)
('i', 1329)
('j', 37)
('k', 143)
('l', 721)
('m', 388)
('n', 1102)
('o', 1336)
('p', 263)
('q', 7)
('r', 630)
('s', 940)
('t', 1121)
('u', 540)
('v', 47)
('w', 304)
('x', 15)
('y', 409)
('z', 16)