EX07 - Functions


In Exercise 07, we’ll be constructing 2 fun functions in 1 python file.

0. Pull the skeleton code

You will find the starter files needed by “pulling” from the course workspace repository. Before beginning, be sure to:

  1. 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.
  2. 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.
  3. Click the Ellipses in the Source Control pane and select “Pull” from the drop-down menu. This will begin the pulling process from the course repository. It should silently succeed.
  4. Return to the File Explorer pane and open the exercises directory. You should see it now contains the directory named ex07. If you expand those directories, you will see the starter file for this exercise.

If the above did not work, try the following:

  1. Click the Ellipses in the Source Control pane and select “Pull, Push” from the drop-down menu. Then select “Pull from”. Then select “upstream” and the main option. This will begin the pulling process from the course repository. It should silently succeed.
  2. Return to the File Explorer pane and open the exercises directory. You should see it now contains another directory named ex07. If you expand that directory, you should see the starter files

Part 1. Primes

Inside your ex07 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:

  1. It has one parameter, of type int.
  2. 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.ex07.primes

You should see True or False depending on the number you passed to the is_prime function. Some examples:

is_prime(3) returns True is_prime(6) returns False is_prime(31) returns True is_prime(110) returns False

list_primes

Now, create a function below your is_prime function called list_primes.

  1. It takes in two parameters, both of type int.
  2. It returns a list[int].

Your function should return a List of all prime numbers between the first int argument and the 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:

The results of which would look like:

$ python -m exercises.ex07.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.

  1. Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
  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, “Progress on Exercise 3” will suffice.
  5. Press the Check icon to make a Commit (a version) of your work.
  6. 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 “EX07 - More Functions + Lists”. 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 ex07, type the following command (all on a single line):

python -m tools.submission exercises/ex07

In the file explorer pane, look to find the zip file named “21.mm.dd-hh.mm-exercises-ex07.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.