![]() |
![]() |
|
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
Last time we saw a loop where we modified a variable each time through:
def stringTimes( numCopies, txtToRepeat ): stringSoFar = "" # one-time setup for currentCopyNumber in range(numCopies): stringSoFar = stringSoFar + txtToTRepeat return stringSoFar |
We say that
Separately, we also noted that the variable
def stringTimes_v2( numCopies, txtToRepeat ): stringSoFar = "" # one-time setup for currentCopyNumber in range(numCopies): stringSoFar = stringSoFar + txtToTRepeat + str(currentCopyNumber) return stringSoFar |
YOUR TASKS:
Write a function
Hints that apply to any loop problem:
To solve the problem, think of a specific input (like “
(This is what we did in yesterday's lab.)
Recall the code for
# substring : string, int, int → string # Given some text, a start-index, and a stop-index, # return the characters of `txt` from `start` up until (but not including) `stop`. # Indices are 0-based. # Examples: substring( "radford", 3, 6) = "for" # substring( "radford", 0, 3) = "rad" # def substring(txt, start, stop): return txt[start:stop] |
Write a function
Hint: see Hint from above, which applies to all loop-problems.
############# Test cases; place your code above these lines ############## # test : None -> None # Run tests for `stutter` and `stutter2`, printing actual and expected results. # def test(): testStutter() testStutter2() # testStutter : None -> None # Run tests for `stutter`, printing actual and expected results. # def testStutter(): print( "Testing `stutter`:" ) print( "Actual: " + stutter("") ) print( "Expect: " + "" ) print( "" ) print( "Actual: " + stutter("a") ) print( "Expect: " + "aa" ) print( "" ) print( "Actual: " + stutter("TODO: REPLACE THIS WITH A SHORT INPUT (but longer than the previous test)") ) print( "Expect: " + "TODO: REPLACE THIS WITH THE EXPECTED OUTPUT" ) print( "" ) print( "Actual: " + stutter("TODO: REPLACE THIS WITH A MEDIUM-LENGTH INPUT") ) print( "Expect: " + "TODO: REPLACE THIS WITH THE EXPECTED OUTPUT" ) print( "" ) print( "Actual: " + stutter("aaa") ) print( "Expect: " + "aaaaaa" ) print( "" ) # testStutter2 : None -> None # Run tests for `stutter2`, printing actual and expected results. # def testStutter2(): print( "Testing `stutter2`:" ) print( "Actual: " + stutter2("") ) print( "Expect: " + "" ) print( "" ) print( "Actual: " + stutter2("ab") ) print( "Expect: " + "abab" ) print( "" ) print( "Actual: " + stutter("TODO: REPLACE THIS WITH A SHORT INPUT (but longer than the previous test)") ) print( "Expect: " + "TODO: REPLACE THIS WITH THE EXPECTED OUTPUT" ) print( "" ) print( "Actual: " + stutter("TODO: REPLACE THIS WITH AN INPUT OF *ODD* LENGTH") ) print( "Expect: " + "TODO: REPLACE THIS WITH THE EXPECTED OUTPUT" ) print( "" ) print( "Actual: " + stutter2("a") ) print( "Expect: " + "" ) print( "" ) print( "Actual: " + stutter2("aaaa") ) print( "Expect: " + "aaaaaaaa" ) print( "" ) print( "Actual: " + stutter2("aaa") ) print( "Expect: " + "aaaa" ) print( "" ) |
1 More accurately, we might say that the accumulator variables hold exactly: the information from past-iterations which is needed to compute the answer, given the remaining loop-iterations. ↩
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Apr.07 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |