Skip to content

Instantly share code, notes, and snippets.

@brendanberg
Created August 6, 2010 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendanberg/510742 to your computer and use it in GitHub Desktop.
Save brendanberg/510742 to your computer and use it in GitHub Desktop.
Counts vowels and consonants from stdin
import sys, string
def vowels(str):
count = 0
for let in str:
if let in list("AEIOUaeiou"):
count += 1
return count
def vowelies(str):
count = 0
for let in str:
if let in list("AEIOUYaeiouy"):
count += 1
return count
def consonants(str):
count = 0
for let in str:
if let in list("BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz"):
count += 1
return count
if __name__ == "__main__":
lines = sys.stdin.readlines()
vowel_list = map(vowels,lines)
consonant_list = map(consonants, lines)
vowely_list = map(vowelies, lines)
print("%d vowels" % reduce(lambda(x,y):x+y,vowel_list))
print("%d vowels (with Y)" % reduce(lambda(x,y):x+y,vowely_list))
print("%d consonants" % reduce(lambda(x,y):x+y,consonant_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment