Homework Three

Published

February 27, 2025

Due 2025-03-06 at the start of class

Submit on Gradescope

Objectives

  • Demonstrate your ability to write conditional statements
  • Demonstrate your ability to create recursive functions
  • Demonstrate your comfort manipulating strings

Getting started

For this assignment, we are asking you to write four recursive functions.

Please put all functions into the same file and call the file homework03.py. Getting this name correct will be important for the autograder

Feel free to work through the functions in any order. We do, however, suggest that you take the time to test each one as you go rather than trying to write all four out like they were part of an essay and then testing at the very end. Treat these are four totally separate and distinct problems (which they are).

Submit your solution on Gradescope using the button above. Feel free to resubmit as your complete each problem to check your progress.

Python subset

There are other ways to handle repetition in Python and you will learn them shortly (or you may have seen them elsewhere). For this assignment, all solutions must be written recursively to receive credit.

In some cases, you will be reimplementing functionality that is already present in Python. Unless explicitly permitted in the problem description, the only functions you are allowed are (abs(), bool(), float(), int(), len(), and str()).

Satisfactory vs. Excellence

A solution for these questions that is excellent will have the all of the following qualities

Style

An excellent function will have a docstring that is formatted in the way shown in the lectures. It should include:

  • the purpose of the function
  • the type and purpose of each parameter (if any)
  • the type and meaning of the output (if any)

In addition, you should follow some of the PEP8 guidelines on whitespace. The ones we will be looking at are:

  • no whitespace between names and ( or [ (e.g., f (5) should be f(5) and s [3:5] should be s[3:5])
  • there should be a single space around operators (e.g., x=4+1 should be x = 4 + 1 and y = 3 -2 should be y = 3 - 2)
  • there should be a space after commas, but not before (e.g., f(4 , 5) or f(4,5) should be f(4, 5))

Special cases and requirements

For some of the problems, we have identified special cases or requirements that we have deemed potentially more challenging and not essential to a satisfactory solution. An excellent solution will cover all cases. These cases will be identified in the autograder by tests with * at the end of the title (so make sure you submit frequently as you are working).

Problem 1: String reverse

Write a recursive function called reverse(text) which takes in a string parameter text and returns a new string that is the reverse of the first one.

reverse
Parameters
text str
Return type str

Examples

>>> reverse("abcdef")
'fedcba'
>>> reverse("a")
'a'
>>> reverse("How many examples do you need? It reverses strings")
'sgnirts sesrever tI ?deen uoy od selpmaxe ynam woH'

Problem 2: Find

Write a recursive function that duplicates the functionality of the built in find(source, sub) function. Since you are not creating a new string method, it will need to accept two parameters: source (the string you are searching in) and sub, the substring you are attempting to locate. Like the original find function, yours should return the lowest index where sub is found within source. If the substring is not found, your function should return -1. You are not expected to implement the optional start and end parameters found in the original implementation.

find
Parameters
source str
sub str
Return type int

Examples

>>> find("look in here", "k")
3
>>> find("look in here", "her")
8
>>> find("a different source", "e")
6
>>> find("a different source", "spam")
-1

Problem 3: Sum multiples

Wite a recursive function called sum_multiples(base, count). It has two parameters, base and count. It should return the sum of the first count multiples of base. So, for example, sum_multiples(2, 4) would be equal to \(20\) because \((1*2) + (2*2) + (3*2) + (4*2) = 2 + 4 + 6 + 8 = 20\).

sum_multiples
Parameters
base int
count int
Return type int

Examples

>>> sum_multiple(2, 4)
20
>>> sum_multiples(3,2)
9
>>> sum_multiples(5,10)
275

Problem 4: Zipper

Write a recursive function called zipper(text1, text2). It has two string parameters: text1 and text2. The function should return a new string in which the two strings have been interleaved starting with the first character of text1. For example, zipper('abc', '123') should return 'a1b2c3'. If text1 and text2 are not the same length, then any remaining content is included without interleaving.

zipper
Parameters
text1 str
text2 str
Return type str

Examples

>>> zipper("abc", "123")
'a1b2c3'
>>> zipper("abcdefghij", "1234")
'a1b2c3d4efghij'
>>> zipper("abc", "123456789")
'a1b2c3456789'