Practice with Strings (from 15-110 Spring 2011 Homework 2)

Note:  these problems were originally written for 15-110 Spring 2011 Homework 2.  Of course, you may ignore references to hw2 here (they do not correspond to this semester's hw), and you may also ignore any other out-of-context references.
 

These are optional practice problems to help you prepare for hw2.  Solutions are provided, along with short video tutorials.

Here are the files you'll need:

Note:  This week, you may not use "if" statements (conditionals), "for" or "while" statements (loops), recursion, or collections (lists, dictionaries, etc).  Since we have not covered these yet, this should not be a concern for most of you.  That said, solutions that use "if", "for", "while", etc, will receive half credit for those portions.


  1. rotateStringLeft
  2. rotateStringRight
  3. hasThreeWords
  4. getTimeString

  1. rotateStringLeft
    Write the function rotateStringLeft that takes a string and a non-negative integer, and returns the string rotated that many places to the left.  Here are some test cases for you:
        assert(rotateStringLeft("abcde", 0) == "abcde")
        assert(rotateStringLeft("abcde", 1) == "bcdea")
        assert(rotateStringLeft("abcde", 2) == "cdeab")
        assert(rotateStringLeft("abcde", 3) == "deabc")
        assert(rotateStringLeft("abcde", 4) == "eabcd")
        assert(rotateStringLeft("abcde", 5) == "abcde")
        assert(rotateStringLeft("abcde", 25) == "abcde")
        assert(rotateStringLeft("abcde", 28) == "deabc")

  2. rotateStringRight
    Write the function rotateStringRight that takes a string and a non-negative integer, and returns the string rotated that many places to the right.  Here are some test cases for you:
        assert(rotateStringRight("abcde", 0) == "abcde")
        assert(rotateStringRight("abcde", 1) == "eabcd")
        assert(rotateStringRight("abcde", 2) == "deabc")
        assert(rotateStringRight("abcde", 3) == "cdeab")
        assert(rotateStringRight("abcde", 4) == "bcdea")
        assert(rotateStringRight("abcde", 5) == "abcde")
        assert(rotateStringRight("abcde", 25) == "abcde")
        assert(rotateStringRight("abcde", 28) == "cdeab")


  3. hasThreeWords
    Write the function hasThreeWords that takes a string (which you may assume only contains letters or spaces) and returns true if the string contains exactly three words, with no leading spaces, and with each word separated by exactly one space.
     Here are some test cases for you:
        assert(hasThreeWords("a b c") == True)
        assert(hasThreeWords("this should work") == True)
        assert(hasThreeWords("but this should fail") == False)
        assert(hasThreeWords("this too") == False)
        assert(hasThreeWords("likewise") == False)
        assert(hasThreeWords(" leading space fails") == False)
        assert(hasThreeWords("no double  spaces") == False)


  4. getTimeString
    Write the function getTimeString that takes (as a non-negative integer) the number of minutes since midnight, and returns the time in "hh:mm" format.  Here are some test cases for you:
        assert(getTimeString(0) == "12:00")
        assert(getTimeString(5) == "12:05")
        assert(getTimeString(60) == "01:00")
        assert(getTimeString(65) == "01:05")
        assert(getTimeString(605) == "10:05")
        assert(getTimeString(665) == "11:05")
        assert(getTimeString(725) == "12:05")
        assert(getTimeString(785) == "01:05")
        assert(getTimeString(665 + 60*12*1000) == "11:05")