Computer Science 15-110, Spring 2011
Class Notes: Conditionals and Loops Practice (Problem-Solving)
- Some Basic Functions
- isSortedString
- findNthVowel
- printSquare
- printDiamond
- nthPrimeNumber
- Hangman
- Sample Solutions
Conditionals and Loops Practice (Problem-Solving)
- Some Basic Functions
- isSortedString
Write
the function isSortedString that takes a string and returns True if
that string is sorted either in non-increasing or non-decreasing order
(according to the Unicode values of the characters). Here are
some test cases and some starter code for you:
def isSortedString(s):
# you write this!
def testIsSortedString():
print "testingIsSortedString()...",
assert(isSortedString("CMU") == True)
assert(isSortedString("UPMC") == True)
assert(isSortedString("MIT") == False)
assert(isSortedString("CCMMUU") == True)
assert(isSortedString("CMUCMU") == False)
assert(isSortedString("A") == True)
assert(isSortedString("") == True)
print "Passed!"
testIsSortedString()
- findNthVowel
Write
the function findNthVowel that takes a string and an integer N and
returns the index of the Nth vowel in the string, or -1 if there are
not N vowels in the string. Vowels are "A", "E", "I", "O", and
"U" (in upper or lower case). Here are some test cases and some
starter code for you:
def findNthVowel(s, n):
# you write this!
def testFindNthVowel():
print "Testing findNthVowel()...",
assert(findNthVowel("AB", 0) == 0)
assert(findNthVowel("BA", 0) == 1)
assert(findNthVowel("BA", 1) == -1)
assert(findNthVowel("ABCDEABCDE", 0) == 0)
assert(findNthVowel("ABCDEABCDE", 1) == 4)
assert(findNthVowel("ABCDEABCDE", 2) == 5)
assert(findNthVowel("ABCDEABCDE", 3) == 9)
assert(findNthVowel("ABCDEABCDE", 4) == -1)
assert(findNthVowel("eBCDoIBCDu", 3) == 9)
assert(findNthVowel("eBCDoIBCDu", 4) == -1)
print "Passed!"
testFindNthVowel()
- printSquare
Write
the function printSquare that takes a non-negative integer N and prints
an NxN square of asterisks ("*"). This function is not easily
robotically testable (why not?).
- printDiamond
Write the function printDiamond that takes a non-negative integer N and
prints an NxN diamond (rotated square) of asterisks ("*"). This function is also not easily
robotically testable.
- nthPrime
Write
the function nthPrime that takes a non-negative integer N and returns
the Nth prime number. If N is negative, return -1. Here are
some test cases and some starter code for you:
def nthPrime(n):
# you write this!
def testNthPrime():
print "Testing nthPrime()...",
assert(nthPrime(0) == 2)
assert(nthPrime(1) == 3)
assert(nthPrime(2) == 5)
assert(nthPrime(3) == 7)
assert(nthPrime(4) == 11)
assert(nthPrime(5) == 13)
assert(nthPrime(6) == 17)
assert(nthPrime(7) == 19)
assert(nthPrime(8) == 23)
assert(nthPrime(1000) == 7927) # some would say this is the 1001st
print "Passed!"
testNthPrime()
- Hangman
Write the function playHangman, which takes no parameters and plays a game of Hangman.
Seeing as we have not yet covered random numbers or lists, we
will provide you with the function getRandomWord(), which returns a
randomly-chosen word (from a list of words that supposedly are good
choices for playing Hangman):
# Include all this code in your Hangman solution.
# Note that the words are in white text here to not give them away
# (though you'll see them briefly when you copy-paste this text...)
import time
def getRandomWord():
# We have not yet covered lists, so you are not responsible for
# this function. But we will use it in our Hangman game.
words = [ "abbey","abruptly","affix","askew","axiom","azure",
"bagpipes","bandwagon","banjo","bayou","blitz","bookworm",
"boxcar","boxful","buckaroo","buffalo","buffoon","cobweb",
"croquet","daiquiri","disavow","duplex","dwarves",
"equip","exodus","fishhook","fixable","foxglove","galaxy",
"galvanize","gazebo","gizmo","glowworm","guffaw","haiku",
"haphazard","hyphen","icebox","injury","ivory","ivy",
"jaundice","jawbreaker","jaywalk","jazzy","jigsaw",
"jiujitsu","jockey","jovial","joyful","juicy","jumbo",
"kazoo","keyhole","khaki","kilobyte","kiosk","kiwifruit",
"knapsack","larynx","luxury","marquis","megahertz",
"microwave","mystify","nightclub","nowadays","numbskull",
"ovary","oxidize","oxygen","pajama","peekaboo","pixel",
"pizazz","pneumonia","polka","quartz","quiz","quorum",
"razzmatazz","rhubarb","rickshaw","schizophrenia","sphinx",
"spritz","squawk","subway","swivel","topaz","unknown",
"unworthy","unzip","uptown","vaporize","vodka","vortex",
"walkway","waltz","wavy","waxy","wheezy","whiskey",
"whomever","wimpy","wizard","woozy","xylophone",
"yachtsman","yippee","youthful","zephyr","zigzag",
"zilch","zodiac","zombie"
]
# Also, we can't use Python's random functions until CLEESE 0.11, so
# we'll use a not-so-random way to choose a number from 0 to N-1:
n = len(words)
randIndex = int(100*time.time()) % n
return words[randIndex]
- Sample Solutions
Here are some sample solutions to the problems in these notes.
carpe diem -
carpe diem - carpe diem - carpe diem
- carpe diem - carpe diem -
carpe diem - carpe diem - carpe
diem