Open the course workspace in VS Code (File > Open Recent > comp110-workspace) and open the File Explorer pane. Expand exercises
.
Right click on the word exercises
and select “New Folder”.
Name the folder exactly: ex04
and create a new file called quiz_redo.py
.
is_tar
Define a function named is_tar
that is given a str
and returns True
if the str
is made up of a "t"
, followed by any number of "a"
s, and ends in an "r"
, otherwise returns False
.
Example usage in the REPL:
>>> from exercises.ex04 import is_tar
>>> is_tar("t")
False
>>> is_tar("tr")
False
>>> is_tar("rat")
False
>>> is_tar("tar")
True
>>> is_tar("taaaaaar")
True
>>> is_tar("taaaaaaaaaaaaar")
True
>>> is_tar("taaaaaaaaaaaaaaaaaaaaar")
True
Your implementation should not make use of any string methods or any features of Python not yet discussed in COMP110.
boot
Define a function named boot
. It is given a string and two integers. It returns a string where the span of characters contained within the range of indices between the two indices are not included in the returned string.
You can assume the first integer parameter’s value will be less than or equal to the second’s.
Example usage in the REPL:
>>> from exercises.ex04 import boot
>>> boot("hello", 2, 3)
'heo'
>>> boot("comp110", 3, 5)
'com0'
>>> boot("0123456789", 2, 2)
'013456789'
>>> boot("0123456789", 2, 4)
'0156789'
>>> boot("0123456789", 4, 6)
'0123789'
>>> boot("0123456789", 2, 6)
'01789'
>>> boot("0123456789", 4, 8)
'01239'
>>> boot("0123456789", 2, 9)
'01'
Your implementation should not make use of any string methods or any features of Python not yet discussed in COMP110. Simple while
loop(s) will suffice.
sum_inputs
Define a function named sum_inputs
. It should take no arguments and return a string. The function will repeatedly prompt the user for numbers and sum each number input up until the user inputs -1
causing the total sum to be returned by the function in the string format shown below.
>>> from exercises.ex04 import sum_inputs
>>> sum_inputs()
Enter an int, -1 to sum: 1
Enter an int, -1 to sum: 3
Enter an int, -1 to sum: -1
'Sum is 4'
>>> sum_inputs()
Enter an int, -1 to sum: 1
Enter an int, -1 to sum: 3
Enter an int, -1 to sum: 5
Enter an int, -1 to sum: -1
'Sum is 9'
Notice the first time sum_inputs
is called, the user enters 1
then 3
and the sum is 4
. The final -1
is ignored by the summation and only used to indicate the prompting should end. In the next sum_inputs
evaluation, the user
You should use the following input
call to prompt the user:
input("Enter an int, -1 to sum: ")
You can assume the user will only enter strings that can be converted to integers without error.
strip
Define a function named strip
that is given a string and a side of the string, either "left"
or "right"
, as arguments to produce a new string without any leading spaces on the left-side of the string or trailing spaces on the right side of the string, based on the second argument. Refer to the example usage below:
>>> from exercises.ex04 import strip
>>> demo: str = " hello "
>>> demo
' hello '
>>> strip(demo, "left")
'hello '
>>> strip(demo, "right")
' hello'
Notice the demo
string has multiple spaces on either side of the characters hello
. In the first call to strip
, "left"
is given as the second argument and the value returned has all of the leading spaces removed from the left-hand of the input string. In the second example, "right"
is given as the second argument and the value returned has all trailing spaces removed from the returned string.
You should not use any built-in string methods (such as the similarly named strip
, rstrip
, or lstrip
methods) in this function.
You should make use of named constants rather than hard-coding any string literals into your strip
function definition.
Be sure to make good choices of meaningful variable names. Your variable names should be descriptive of their purposes. You should also declare your variables with explicit types.
Once your program is working, add a docstring at the top of your file with a one-sentence description of your program’s purpose.
Then, add your __author__
variable assigned to your name and e-mail address.