Homework Three (retake)
Due 2025-03-28 at 11:59p
Submit on GradescopeThis is your second shot at the problems from Homework Three. If you missed any of the problems, you are strongly advised to try again. Your goal is to complete (Satisfactory or Excellent) up to four problem across Homework Three and this set, but you are encouraged to do more – especially if you were challenged by the first set of problems. Remember that we evaluate each problem individually, so there are no issues if you only do a subset of the problems (including none).
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 bef(5)
ands [3:5]
should bes[3:5]
) - there should be a single space around operators (e.g.,
x=4+1
should bex = 4 + 1
andy = 3 -2
should bey = 3 - 2
) - there should be a space after commas, but not before (e.g.,
f(4 , 5)
orf(4,5)
should bef(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: Mirror
Write a recursive function called mirror(text)
that takes one string argument text
. it should return a new string that creates a palindrome out of the string, flipping it and repeating it. The final letter of the word should serve as the pivot and is not repeated. For example, mirror('mirror')
would return 'mirrororrim'
.
mirror
|
|||
---|---|---|---|
Parameters |
|
||
Return type |
str
|
Examples
>>> mirror("ab")
'aba'
>>> mirror("mirror")
'mirrororrim'
>>> mirror("fairest")
'fairestseriaf'
Problem 2: Replace
Write a recursive function called replace(text, old, new)
. It has three string parameters: text
, old
, and new
. Your function should return a copy of text
in which all copies of old
have been replaced with new
.
replace
|
|||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Return type |
str
|
Examples
>>> replace("replace", "a", "@")
'repl@ce'
>>> replace("one fish, two fish, red fish, blue fish", "f", "d")
'one dish, two dish, red dish, blue dish'
>>> replace("one fish, two fish, red fish, blue fish", "fish", "dalek")
'one dalek, two dalek, red dalek, blue dalek'
>>> replace("one fish, two fish, red fish, blue fish", "carp", "dalek")
'one fish, two fish, red fish, blue fish'
Problem 3: Product
Write a recursive function called product(m, n)
, which has two integer parameters m
and n
. The function should return the product of m
and n
. The catch is that you are not allowed to use multiplication.
product
|
|||||
---|---|---|---|---|---|
Parameters |
|
||||
Return type |
int
|
Examples
>>> product(18, 1)
18
>>> product(3, 5)
15
>>> product(5, 3)
15
>>> product(6, 7)
42
>>> product(5, -2)
-10
Problem 4: Make sequence
Write a recursive function called make_sequence(n, ascending)
. The function has one positive integer parameter (n
) and one boolean parameter, (ascending
). This function returns a string containing a space separated list of numbers which count from 0 to n
inclusive when ascending
is True
and from n
to 0 inclusive when ascending
is false.
make_sequence
|
|||||
---|---|---|---|---|---|
Parameters |
|
||||
Return type |
str
|
Examples
>>> make_sequence(5, True)
'0 1 2 3 4 5'
>>> make_sequence(5, False)
'5 4 3 2 1 0'
>>> make_sequence(25, False)
'25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0'