CMU 15-112 Summer 2018: Fundamentals of Programming and Computer Science
Homework 4 (Due Tue 29-May, at 5pm)
- This assignment is SOLO. This means you may not look at other student's code or let other students look at your code for these problems. See the syllabus for details.
- To start:
- Create a folder named 'week1'
- Download hw4.py to that folder
- Edit hw4.py using Pyzo
- When you are ready, submit hw4.py to Autolab. For this hw, you may submit up to 40 times, but only your last submission counts.
- Do not use recursion in this assignment.
- Do not hardcode the test cases in your solutions.
- Note: The autograder is not yet released for this assignment.
- This assignment gives you very few test cases in the starter file. You'll want to add your own!
- Piazza Post [5pts]
Make a post to Piazza by the hw4 deadline with a 112 related question.
- 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.
- 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]
- 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).