Homework Two (retake)
Due 2025-03-11 at the start of class
Submit on GradescopeThis is your second shot at the problems from Homework Two. 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 Two 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 functions involving the user of parameters and return statements
- Demonstrate your 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_retake.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. Resubmit to the autograder early and often.
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: Add tag
Web pages are written in HTML (HyperText Markup Language). It is called a “markup language” because it wraps content in tags to provide information about the structure and appearance of the page. For example, the make text italic, we would wrap it in an <em>
tag (em
for “emphasis”). We “wrap” content by putting a start tag at the beginning (e.g., <em>
) and an end tag at the end (e.g., </em>
). Note that the only difference between a start tag and an end tag is the addition of the /
before the tags name.
Write a function called add_tag
, which takes in two string arguments: tag
and text
. It should return a new string consisting of the original text wrapped by the tag.
add_tag
|
|||||
---|---|---|---|---|---|
Parameters |
|
||||
Return type |
str
|
Examples
>>> add_tag("em", "emphasize this")
'<em>emphasize this</em>'
>>> add_tag("strong", "be bold, my friend")
'<strong>be bold, my friend</strong>'
>>> add_tag("p","p stands for paragraph")
'<p>p stands for paragraph</p>'
Problem 2: Leap year checker
Write a function called is_leapyear
which takes a single integer parameter called year
. The function should return a boolean indicating if the year is a leap year or not. Leap years come almost every four years, so years that are multiples of 4 are leap years. The exception is years that are divisible by 100, which are not. The exception to the exception is years that are divisible by 400 are leap years.
is_leapyear
|
|||
---|---|---|---|
Parameters |
|
||
Return type |
bool
|
Examples
>>> is_leapyear(1500)
False
>>> is_leapyear(1600)
True
>>> is_leapyear(2000)
True
>>> is_leapyear(2025)
False
>>> is_leapyear(2020)
True
Problem 3: Right justify
Write a function called right_justify
which takes in two parameters: a string text
and an integer width
. The goal of this function is to right justify the string. The width
variable provides length of the returned string. For the text to be right justified in the string, it has to end at the end of the output string. We will not give you a width
that cannot fit the string.
right_justify
|
|||||
---|---|---|---|---|---|
Parameters |
|
||||
Return type |
string
|
Examples
>>> right_justify("a", 10)
' a'
>>> right_justify("abc", 10)
' abc'
>>> right_justify("abcdef", 10)
' abcdef'
>>> right_justify("abcdef", 15)
' abcdef'
Problem 4: # Number flip
Write a function called number_flip
which takes in an integer parameter called n
. The function should return a new integer with all of the digits of the original reversed. If the original value was negative, the returned value should be as well. n
is guaranteed to be non-zero.
It may be useful to know that there is a quick trick for reversing strings using slicing. There is a third value we can include in our slice which is the step to use between characters. If we set this to -1 (e.g., s[::-1]
) it will walk backwards through the string, reversing it.
number_flip
|
|||
---|---|---|---|
Parameters |
|
||
Return type |
int
|
Examples
>>> number_flip(42)
24
>>> number_flip(250)
52
>>> number_flip(-64)
-46