RU beehive logo ITEC dept promo banner
ITEC 109
2014spring
ibarland

homeinfolabsexamshws
D2LMediaSamples/breeze (snow day)tutor/PIs

lab34
loop practice
accumulating a string

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 stringSoFar is an “accumulator” variable (or a “so-far” variable), because it accumulates info1 each time we repeat the loop.

We say that currentCopyNumber is the “index variable”.

Separately, we also noted that the variable currentCopyNumber in the above code was a bit odd: we never actually used the value of that variable! We did discuss what happens in the following variation though:

def stringTimes_v2( numCopies, txtToRepeat ):
  stringSoFar = ""  # one-time setup
  for currentCopyNumber in range(numCopies):
    stringSoFar = stringSoFar + txtToTRepeat + str(currentCopyNumber)
  return stringSoFar
By walking through the program step-by-step, you should be able to figure out how/why stringTimes_v2( 4, "hi" ) returns "hi0hi1hi2hi3".

YOUR TASKS:

  1. Write a function stutter, which builds a string which has every character of the input repeated: For example, stutter("cats") = "ccaattss". See more tests below.

    Hints that apply to any loop problem: To solve the problem, think of a specific input (like “"cats"”), and consider these questions:

    Recall the code for substring:

    # 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]
    

  2. Write a function stutter2, which builds a string which has every pair of characters of the input repeated: For example, stutter2("cats") = "cacatsts". See more tests below.

    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.      

homeinfolabsexamshws
D2LMediaSamples/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 ibarlandradford.edu
Powered by PLT Scheme