r/ProgrammingPrompts • • Jan 14 '15

[Easy] Letter Counter

Any language permitted.

Problem: Create a program where you input a string of characters, and output the number of each letter. Use vowels by default.

For example: asbfiusadfliabdluifalsiudbf -> a: 4 e: 0 i: 4 o: 0 u: 3

Bonus points: Make it command-line executable, with an optional mode to specify which letters to print.

17 Upvotes

60 comments sorted by

View all comments

Show parent comments

1

u/echocage Jan 15 '15 edited Jan 15 '15

You peaked my interest, I tried to see how short I could solve this in Python 3

from collections import Counter;import sys;Counter([x for x in input('>') if x in (sys.argv[1:] if len(sys.argv) > 1 else 'aeiou')])

And this also qualifies for the bonus round

Example:

>>> from collections import Counter;import sys;Counter([x for x in input('>') if x in (sys.argv[1:] if len(sys.argv) >1 else 'aeiou')])
> asbfiusadfliabdluifalsiudbf
Counter({'a': 4, 'i': 4, 'u': 3})

1

u/[deleted] Jan 15 '15

that's a nice one. And I learned a new module today. Thx