Skip to content

Instantly share code, notes, and snippets.

@duskcheetah
Created September 20, 2010 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save duskcheetah/587261 to your computer and use it in GitHub Desktop.
Save duskcheetah/587261 to your computer and use it in GitHub Desktop.
# displays alphanumeric character count and sum of digits in file
import sys
if len(sys.argv) < 2:
print "Usage: python charcount.py <input.txt>"
exit()
f = open(sys.argv[1])
count_dict = {}
for line in f:
c = line.strip()
if c in count_dict:
count_dict[c] = count_dict[c] + 1
else:
count_dict[c] = 1
for key in sorted(count_dict):
print key, ": ", count_dict[key]
total = 0
for i in range(1,9):
total += i * count_dict[str(i)]
print "Sum: ", total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment