In Exercise 03, you will write four fun functions across three Python modules.
0. Pull the skeleton code
You will find the starter files needed by “pulling” from the course workspace repository. Before beginning, be sure to:
- Be sure you are in your course workspace. Open the file explorer and you should see your work for the course. If you do not, open your course workspace through File > Open Recent.
- Open the Source Control View by clicking the 3-node (circles) graph (connected by lines) icon in your sidebar or opening the command palatte and searching for Source Control.
- Click the Ellipses in the Source Control pane and select “Pull, Push” from the drop-down menu, then select “Pull from…” A box will appear and you should select either “origin” or “upstream”, but not “backup”. This will begin the pulling process from the course repository. It should silently succeed. (If you are on macOS and do not see anything in source control anymore, it may be because of a macOS update. You can typically resolve this by opening a new Terminal, typing the command
xcode-select --install
, pressing enter and following its instructions.) You will also need to restart VSCode after doing this. - Return to the File Explorer pane and open the
exercises
directory. You should see it now contains the directory namedex03
. If you expand that directory, you will see the starter files for the three Python programs in this exercise.
1. Avoid Fifth
In summary, this function gains its inspiration from a subforum which allows any post as long as it avoids the fifth symbol in our ABC’s: E
. For a short duration, you will stand with this community and try to cut out that fifth glyph from all communications. Try it yourself, it’s fairly tough. To aid with this goal, you will construct a function that will abolish that symbol from a particular str
.
Phew, that was exhausting. Now let’s get to programming.
avoid_fifth
Inside your ex03
folder, you should see a file named avoid_fifth.py
. Inside you’ll see the skeleton of a main function and a call at the bottom following the if __name__ == "__main__"
idiom.
Define a function in the avoid_fifth.py
file called avoid_fifth
with the following specifications:
avoid_fifth
has one parameter of typestr
.avoid_fifth
returns astr
value.
The function should loop through the given str
using string-indexing and output a str
without any e’s in it.
Notes:
- A letter can be in both lowercase and uppercase.
avoid_fifth
should be able to remove both. - You may not use the built in string .replace() method.
Here is an example of how you should test your work with the main()
:
def main():
"""Entrypoint of the program."""
print(avoid_fifth("hello"))
print(avoid_fifth("The sentence we have here possesses E's galore!"))
And in your terminal:
$ python -m exercises.ex03.avoid_fifth
hllo
Th sntnc w hav hr posssss 's galor!
2. Palindromify
A palindrome is a word that reads the same way forwards and backwards. For example, the english words ‘deified’ or ‘noon’ or ‘madam’ would stay the same if you put the last letter first, second to last letter second, and so on.
palindromify
Inside your ex03
folder, you should see a file named palindromify.py
. Inside you’ll see the skeleton of a main function and a call at the bottom following the if __name__ == "__main__"
idiom.
Define a function in palindromify.py
called palindromify
with the following specifications:
- palindromify has two parameters of type
str
andbool
. - palindromify returns a
str
value.
Given a particular string, such as “deif”, palindromify
will transform it into a palindrome: “deified”
However, there is a slight complication. Palindromes can be either ‘even’ or ‘odd’ in length. The word “deified”, for example, has an odd number of letters and flips around the letter ‘f’ in the middle. But there are palindromes of even length, such as “noon”, which flip in between the repeated character “o”.
To solve this problem, you have a second parameter which is a bool
that tells the function whether a string needs to be ‘palindromified’ in an even form (True
) or in an odd form (False
).
Notes:
- Try to implement the ‘even’ (
True
) palindrome case first and mirror every letter before moving on to the ‘odd’ (False
) version.
Like avoid_fifth, this function will requires familiarity string-indexing and concatenation as well as conditional if-else statements. Your finished implementation should work like this with the following examples:
print(palindromify("race", False))
print(palindromify("han", True))
print(palindromify("red", True))
print(palindromify("live on time ", False))
$ python -m exercises.ex03.palindromify
racecar
hannah
redder
live on time emit no evil
3. Primes
Inside your ex03
folder, you should see a file named primes.py
. Inside you’ll see the skeleton of a main function and a call at the bottom following the if __name__ == "__main__"
idiom.
is_prime
Create a function in your primes.py
file called is_prime
. It has the following specifications:
- It has one parameter, of type
int
. - It returns a
bool
value.
The bool
it returns will be True
if the argument to the function is prime, and False
if it is not. If the int
argument provided is less than or equal to 1, return False
.
Notes:
- A number is not prime if it can be evenly divided by a number other than 1 and itself. The
%
operator may be useful for determining this. - Think about how you can use a
while
loop to test factors sequentially.
Before moving on, you can check whether or not is_prime
works by calling it in your main
function and printing the result. When you run the program:
python -m exercises.ex03.primes
You should see True
or False
depending on the number you passed to the is_prime
function. Some examples:
For these examples, 3 is prime, 6 is not prime, 31 is prime, 110 is not prime
list_primes
Now, create a function below your is_prime
function called list_primes
.
- It takes in two parameters, both of type
int
. - It returns a
list[int]
.
Your function should return a list
of all prime numbers between the first int
argument and he second int
argument, inclusive of the first but exclusive of the second. Namely, list_primes(3, 7)
would potentially include 3, but wouldn’t include 7.
Notes:
- You may assume that the first argument will be less than or equal to the second argument.
- You should make use of your
is_prime
function in this function to help you check each number.
As with is_prime
, you can check the correctness of your list_primes
function by calling it in main
, printing the result, then running the program with the same command from above.
Some examples:
print(list_primes(3, 7))
print(list_primes(10, 20))
print(list_primes(25, 28))
print(list_primes(-1, 5))
The results of which would look like:
$ python -m exercises.ex03.primes
[3, 5]
[11, 13, 17, 19]
[]
[2, 3]
4. Make a Backup Checkpoint “Commit”
As you make progress on this exercise, making backups is encouraged. Note that you do not have to make a backup in order to submit your work, though you are encouraged to before each submission so that you can revert back to a previous point in your project if you accidentally change something you did not intend to.
- Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
- Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
- 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.
- 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, “Progress on Exercise 3” will suffice.
- Press the Check icon to make a Commit (a version) of your work.
- Finally, press the Ellipses icon (…), look for “Pull/Push” submenu, and select “Push to…”, and in the dropdown select your backup repository.
5. Submit to Gradescope for Grading
Login to Gradescope and select the assignment named “EX03 - More Functions”. 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 at the bottom of your screen, open the Command Palette and search for “View: Toggle Integrated Terminal”.
To produce a zip file for ex03
, type the following command (all on a single line):
python -m tools.submission exercises/ex03
In the file explorer pane, look to find the zip file named “21.mm.dd-hh.mm-exercises-ex03.zip”. The “mm”, “dd”, and so on, are timestamps 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. For this exercise there will be points manually graded for style – using meaningful variable names and snake_case. 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!