EX04 - Well, well, we meet again.


Introduction

In this exercise you will work through a few familiar looking problems. You first met these problems on Quiz 00. This exercise is only an exercise. For grading purposes, this exercise is completely unrelated to the work handed in on Quiz 00 and any work you do on EX04 has no bearing on work for QZ00.

If you requested to withdraw your Quiz 00 submission you should start your work over and pay no attention to your previous work. We are not removing Quiz 00 submissions that were withdrawn until after the deadline of this assignment.

Setup Exercise Directory

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.

Part 1 - 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.

Part 2 - 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.

Part 3 - 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.

Part 4 - 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 are not permitted to 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.

Style and Documentation Requirements

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 an __author__ variable assigned your PID as a string value after your docstring.

Make a Backup Commit

  1. Open the Source Control panel: View > SCM.
  2. Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
  3. Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
    • If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
  4. In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Finished Exercise 1!” will suffice.
  5. Press the Check icon to make a Commit (a version) of your work.
  6. Finally, press the Ellipses icon (…) and select “Push” to send this backed up version to your workspace repository space on GitHub.

Submit to Gradescope for Grading

All that’s left now is to hand-in your work on Gradescope for grading!

Remember, before an assignment’s deadline you can resubmit work as many times as you need to without penalty. Portions of assignments are autograded and will provide near-immediate feedback. We want you to resubmit as many times as it takes you in order to earn full autograding credit!

Login to Gradescope and select the assignment. You’ll see an area to upload a zip file. To produce a zip file for autograding, return back to Visual Studio Code.

If you do not see a Terminal, open a new Terminal.

Type the following command (all on a single line):

python -m tools.submission exercises/ex04

In the file explorer pane, look to find the zip file named “yy.mm.dd-hh.mm-submission.zip”. The “mm”, “dd”, and so on, are timestamped with the current month, day, hour, minute. If you right click on this file and select “Reveal in File Explorer” on Windows or “Reveal in Finder” on Mac, the zip file’s location on your computer will open. Upload this file to Gradescope to submit your work for this exercise.

Autograding will take a few moments to complete. If there are issues reported, you are encouraged to try and resolve them and resubmit. If for any reason you aren’t receiving full credit and aren’t sure what to try next, come give us a visit in office hours!