Skip to content

Instantly share code, notes, and snippets.

@jjlumagbas
Created October 10, 2016 02:36
Show Gist options
  • Save jjlumagbas/47adbb081c638fd617a9ed5f285bb25e to your computer and use it in GitHub Desktop.
Save jjlumagbas/47adbb081c638fd617a9ed5f285bb25e to your computer and use it in GitHub Desktop.
def is_vowel(ch):
"""String -> Bool
Determines whether a single char string
is a vowel
"""
return ch == "a" or ch == "e" or ch == "i" or ch == "o" or ch == "u" or ch == "A" or ch == "E" or ch == "I" or ch == "O" or ch == "U"
def remove_vowels(s):
"""String -> String
Remove all vowels from s
"""
new_string = ""
for ch in s:
if (not is_vowel(ch) and ch != " "):
new_string = new_string + ch
return new_string
print(remove_vowels("Hello world!")) # Helloworld!
print(remove_vowels("He llo wor ld!")) # Helloworld!
print(remove_vowels("He llo wor ld!")) # Helloworld!
# Write the following function that returns
# a string that is the same as s except that
# spaces are removed.
def remove_spaces(s):
"""String -> String
Remove all spaces from s
"""
new_string = ""
for ch in s:
if (ch != " "):
new_string = new_string + ch
return new_string
print(remove_spaces("Hello world!")) # Helloworld!
print(remove_spaces("He llo wor ld!")) # Helloworld!
print(remove_spaces("He llo wor ld!")) # Helloworld!
# Write the following function that returns
# the number of vowels in string s. Both uppercase
# and lowercase vowels should be counted.
# (The vowels are a, e, i, o, and u.)
def is_vowel(ch):
"""String -> Bool
Determines whether a single char string
is a vowel
"""
return ch == "a" or ch == "e" or ch == "i" or ch == "o" or ch == "u" or ch == "A" or ch == "E" or ch == "I" or ch == "O" or ch == "U"
print(is_vowel("a")) # True
print(is_vowel("b")) # False
print(is_vowel("e")) # True
print(is_vowel("o")) # True
print(is_vowel("A")) # True
print(is_vowel("E")) # True
def num_vowels(s):
"""String -> Num
Counts the number of vowels in a string
"""
count = 0
for ch in s:
if (is_vowel(ch)):
count = count + 1
return count
print(num_vowels("A")) # 1
print(num_vowels("a")) # 1
print(num_vowels("ab")) # 1
print(num_vowels("aa")) # 2
print(num_vowels("abe")) # 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment