-
-
Save artlung/587273 to your computer and use it in GitHub Desktop.
This file contains 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
# 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