Recursion Practice

Table of Contents

This is a pair programming assignment. If you are working in a pair, this means that you and your partner should be doing the entirety of this assignment side-by-side, on a single computer, where one person is "driving" and the other is "navigating." Set a timer to swap every 15 minutes. You can choose your favorite from this online timer page. Make sure your sound volume is audible, but not so loud to disturb the people around you.

If you are working in a pair, only one of you needs to submit your work via Moodle. That said, you should both have a copy of your work in case you want it someday, so make sure that both of you have copies of it; you can email it or use some other mechanism to transfer it.

We will use anonymous grading on Moodle, which means that the grader won't see your name until after the grading is done. This is an easy way to help add an extra element of fairness to the grading. Therefore, make sure your name doesn't appear on your actual submission. When you submit via Moodle, it will know you are. Thanks!

For all three of these exercises, you must do them recursively.

1 Maximum

listmax(numberlist) should take a list of parameters as a parameter, and return the largest. Hint: the largest in the entire list is the larger of the first item and the listmax of all the other items. You may not use the built in functions max, min, or any other function that would similarly make this trivial.

2 Palindrome

palindrome(word) should return True or False, depending on whether or not the word is a palindrome. A palindrome is a word that reads the same forward or backwards, such as "racecar". You may assume that the word contains only letters, all in lower case, with no spaces or punctuation. You should NOT do this assignment by reversing the string and testing if the reversed string and the original are the same. Hint: check if the first and last letters are the same, then see if what is left is a palindrome.

3 Elements Less Than

getElementsLessThan(value,lst) should take a value, and a list. The function should then return a list containing all values in the original list that are less than value.

4 Some final clarifications

Your code must contain no loops at all. (No for or while loops allowed.) I'm usually asked by students for this assignment if you are allowed to use "an if loop"; this is a fine time to point out that if isn't a loop.

Your code must be recursive, and the recursive code must be relevant for actually solving the problem at hand. The following sample code is an an example of a program that is technically does recursion, but the recursion is entirely irrelevant:

def printWithUselessRecursion(s):
    if len(s) == 1:
        return s
    printWithUselessRecursion(s[0])
    print(s)

Author: Dave Musicant

Emacs 24.5.1 (Org mode 8.2.10)

Validate