Homework Two
Due 2025-02-27 at the start of class
Submit on GradescopeObjectives
- Demonstrate your ability to write functions involving the user of parameters and return statements
- Demonstrate you ability to perform basic string manipulation
- Demonstrate your ability to write basic Python expressions
Getting started
For this assignment, we are asking you to write four functions. The functions require you to perform some basic string manipulation and math.
Please put all functions into the same file and call the file homework02.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.
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)
)
Current knowledge
All of these problems can be solved using just the information you have been provided in class up to the end of the class period on the day this assignment was released. There are concepts we will not have covered (conditional statements in particular, which you will see before the assignment is due) that may make some of the problems a little easier. Correct solutions using them will be satisfactory. An excellent solution will use just the material presented at the time the assignment was released.
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 surround
Write a function called str_surround
which takes in two strings: padding
and word
. The function should return a new string where word has been inserted into the center of padding
. padding
is guaranteed to have an even number of characters.
str_surround
|
|||||
---|---|---|---|---|---|
Parameters |
|
||||
Return type |
str
|
Examples
>>> str_surround("--", "Hello there")
'-Hello there-'
>>> str_surround("<<>>", "Danger")
'<<Danger>>'
>>> str_surround("******", "Run!")
'***Run!***'
>>> str_surround("-<={}=>-", "Open all hours")
'-<={Open all hours}=>-'
Problem 2: Halfway point
Write a function called halfway_point
which finds the midpoint between two numbers x
and y
.
halfway_point
|
|||||
---|---|---|---|---|---|
Parameters |
|
||||
Return type |
float
|
Examples
>>> halfway_point(0, 9)
4.5
>>> halfway_point(3, 7)
5.0
>>> halfway_point(3, -3)
0.0
>>> halfway_point(25, 10)
17.5
Problem 3: Elapsed time
Write a function called elapsed_time
which takes in two strings: start
and end
. The strings will hold times in the format "HH:MM"
(i.e., both hours and minutes will be zero padded if they are less than 10). The times will also be in 24-hour form (i.e., "14:00"
instead of "02:00"
for 2:00 PM). The function should return a string of the form "Elapsed time: H hour(s) and M minute(s)"
where H
is the number of elapsed hours and M
is the number of elapsed minutes. Your output string should match the examples exactly except with respect to the values for the hours and minutes.
You can assume that all times will be well-formed (two digits for both the hour and the minute) and that the start
time will always be less than the end
time.
elapsed_time
|
|||||
---|---|---|---|---|---|
Parameters |
|
||||
Return type |
str
|
Examples
>>> elapsed_time("09:00", "09:15")
'Elapsed time: 0 hour(s) and 15 minute(s)'
>>> elapsed_time("10:30", "12:15")
'Elapsed time: 1 hour(s) and 45 minute(s)'
>>> elapsed_time("00:00", "23:59")
'Elapsed time: 23 hour(s) and 59 minute(s)'
Problem 4: Crazy multiplication
Write a function called crazy_mult()
which multiplies two numbers (x
and y
) together. This doesn’t do normal multiplication however. It will return a number that is formed by y
copies of x
glued together. To add a little complexity, the sign of the result should behave like the sign in normal multiplication (i.e., if the sign of both x
and y
are the same, the result is positive, if they are different then the result is negative). Both numbers are guaranteed to be non-zero.
Hint: Python has a builtin function abs()
that returns the absolute value of a number (e.g., abs(-4)
returns 4
).
crazy_mult
|
|||||
---|---|---|---|---|---|
Parameters |
|
||||
Return type |
int
|
Examples
>>> crazy_mult(3, 12)
333333333333
>>> crazy_mult(42, 3)
424242
>>> crazy_mult(-42, 2)
-4242
>>> crazy_mult(42, -2)
-4242
>>> crazy_mult(-42, -2)
4242