CMU 15-110: Principles of Computing
Unit 3: Case Studies + Further Practice
# Here is the code we wrote in class.
# Some functions were written multiple times, to demonstrate
# different approaches you might take in writing them.
def isVowel(c):
c = c.lower()
if ((c == 'a') or (c == 'e') or (c == 'i') or (c == 'o') or (c == 'u') or (c == 'y')):
return True
else:
return False
def isVowel(c):
c = c.lower()
vowels = 'aeiouy'
if (c in vowels):
return True
else:
return False
def isVowel(c):
return c.lower() in 'aeiouy'
def testIsVowel():
print('Testing isVowel()...', end='')
assert(isVowel('a') == True)
assert(isVowel('u') == True)
assert(isVowel('y') == True)
assert(isVowel('O') == True)
assert(isVowel('q') == False)
assert(isVowel('m') == False)
print('Passed!')
testIsVowel()
def vowelCount(s):
count = 0
for c in s:
if (isVowel(c) == True):
count += 1
return count
def testVowelCount():
print('Testing vowelCount()...', end='')
assert(vowelCount('abc') == 1)
assert(vowelCount('abcdeabcde') == 4)
assert(vowelCount('abcdEABcde') == 4)
assert(vowelCount('') == 0)
assert(vowelCount('aeiouy') == 6)
print('Passed!')
testVowelCount()
def reverseString(s):
result = ''
for c in s:
result = c + result
return result
def testReverseString():
print('Testing reverseString()...', end='')
assert(reverseString('a') == 'a')
assert(reverseString('abc') == 'cba')
assert(reverseString('') == '')
print('Passed!')
testReverseString()
def isPalindrome(s):
for i in range(len(s)):
cLeft = s[i]
cRight = s[len(s)-1-i]
if (cLeft != cRight):
return False
return True
def isPalindrome(s):
if (s == reverseString(s)):
return True
else:
return False
def isPalindrome(s):
return (s == reverseString(s))
def testIsPalindrome():
print('Testing isPalindrome()...', end='')
assert(isPalindrome('a') == True)
assert(isPalindrome('aa') == True)
assert(isPalindrome('aba') == True)
assert(isPalindrome('abcba') == True)
assert(isPalindrome('') == True)
assert(isPalindrome('abc') == False)
assert(isPalindrome('abcb') == False)
assert(isPalindrome('abcbab') == False)
print('Passed!')
testIsPalindrome()