CMU 15-112 Summer 2018: Fundamentals of Programming and Computer Science
Homework 4 (Due Tue 29-May, at 5pm)




  1. Piazza Post [5pts]
    Make a post to Piazza by the hw4 deadline with a 112 related question.

  2. lookAndSay(a) [35pts]
    First, read about look-and-say numbers here. Then, write the function lookAndSay(a) that takes a list of numbers and returns a list of numbers that results from "reading off" the initial list using the look-and-say method, using tuples for each (count, value) pair. For example:
      lookAndSay([]) == []
      lookAndSay([1,1,1]) == [(3,1)]
      lookAndSay([-1,2,7]) == [(1,-1),(1,2),(1,7)]
      lookAndSay([3,3,8,-10,-10,-10]) == [(2,3),(1,8),(3,-10)]
    
    Hint: you'll want to keep track of the current number and how many times it has been seen.

  3. nondestructiveRotateList(a, n) [30pts]
    Write the function nondestructiveRotateList(a, n) which takes a list a and an integer n, and nondestructively modifies the list so that each element is shifted to the right by n indices (including wraparound). The function should then return this new list. For example:
        nondestructiveRotateList([1,2,3,4], 1) -> [4,1,2,3]
        nondestructiveRotateList([4,3,2,6,5], 2) -> [6, 5, 4, 3, 2]
        nondestructiveRotateList([1,2,3], 0) -> [1,2,3]
        nondestructiveRotateList([1, 2, 3], -1) -> [2, 3, 1]
    

  4. destructiveRotateList(a, n) [30pts]
    This function works the same as the previous function, only here it is destructive. That is, it directly changes the list a, so after the call, that exact list is rotated n indices to the right with wraparound, and a new list is not created. As usual for destructive functions, this function returns None. Also: you may not call the nondestructive version here, and in fact, you may not even create a new list (or tuple or other similar data structure) that is longer than 10 elements (you must modify the original list in place).