CMU 15-112 Summer 2018: Fundamentals of Programming and Computer Science
Homework 10 (Due Wed 13-Jun, 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 'week4'
- Edit hw10.py using Pyzo
- When you are ready, submit hw10.py to Autolab. For this hw you will have 20 submissions.
- This homework is fully autograded.
- Do not hardcode the test cases in your solutions.
- This homework may be graded for style.
-
You must use recursion to solve these problems. Using a for or while loop will result in you losing full credit for the question. You may also not use inherently iterative functions. These include range, sum, max, min, count, replace, sort, reverse, sorted, and reversed.
- recursive alternatingSum(lst) [30 pts]
Write the function alternatingSum(lst) that takes a possibly-empty list of numbers, lst, and returns the alternating sum of the list, where every other value is subtracted rather than added. For example: alternatingSum([1,2,3,4,5]) returns 1-2+3-4+5 (that is, 3). If lst is empty, return 0.
- recursive powersOf3ToN(n) [30 pts]
Write the function powersOf3ToN(n) that takes a possibly-negative float or int n, and returns a list of the positive powers of 3 up to and including n, or None (not an empty list) if no such values exist. As an example, powersOf3ToN(10.5) returns [1, 3, 9].
- recursive generateLetterString [40 pts]
Write the function generateLetterString(s) that takes a two-character string and returns a new string that contains the all of the letters between the first letter and the second letter. For example, generateLetterString("ko") would return "klmno". This should also work backwards, so generateLetterString("me") would return "mlkjihgfe". If the initial provided string is not two characters long you should return the empty string. If the string contains two identical characters (like "aa"), then return the letter ("a"). You may assume that you are given only lowercase letters.