CS 111
Summer 2 2018

Problem Set 1

due by 9:00 p.m. on Tuesday, July 03, 2018

Preliminaries

In your work on this assignment, make sure to abide by the collaboration policies of the course.

If you have questions while working on this assignment, please come to office hours, post them on Piazza, or email cs111-staff@cs.bu.edu.

Make sure to submit your work on Apollo, following the procedures found at the end of the assignment.

For each problem in this problem set, we will be writing or evaluating some Python code. In today’s class, we will introduce the Python IDLE development environment.


Problem 1: The four fours challenge

50 points; individual-only

The material needed for this problem will be discussed in class. You can find more information in the pre-lecture readings and videos for the first class on Python.

In this problem you will write a simple Python program that computes the integers 0 through 4 using expressions involving exactly four fours and no other numbers. For example:

zero = 4 + 4 - 4 - 4
print('zero =', zero)

Your expressions may use any of the following operators: +, -, *, // (integer division), ** (power), and parentheses. Note that you should use the integer division operator (//) instead of the regular division operator (/), because the / operator will cause your results to include a decimal. For example, 4//4 will give you 1, but 4/4 will give you 1.0.

Begin by downloading this file: ps1pr1.py

Open ps1pr1.py in IDLE using the File->Open menu option.

We have given you the two lines above for computing 0. You should add in the code needed to compute the integers 1 through 4 using four fours. For each integer, you should (i) assign the result of the computation to an appropriately named variable, and (ii) use a print statement to produce the appropriate output, following the model given for 0.

To test your code, use the Run->Run Module menu option in IDLE. Doing so will take you to a separate window for the Python Shell, where the output of your program will be displayed. The output should appear in a blue font, and it should look like this:

zero = 0
one = 1
two = 2
three = 3
four = 4

Notes:



Problem 2: Indexing and slicing puzzles

50 points; individual-only

This problem will give you practice with indexing and slicing. Begin by downloading this file: ps1pr2.py. Open it in IDLE, as discussed in class.

Highly recommended

  • Use the screen layout described in class, which we’ve reproduced below.
  • Modify IDLE’s settings following the instructions from Task 3 of Lab 1. Doing so will allow you to use the Up Arrow and Down Arrow keys to scroll through the expressions that you’ve already entered in the Shell, and will thus save you a lot of unnecessary typing!

IDLE

List puzzles
The first half of the problem focuses on lists. In particular, you will be constructing new lists from the following:

pi = [3, 1, 4, 1, 5, 9]
e = [2, 7, 1]

We’ve given you these lines in ps1pr2.py. In addition, we’ve provided the answer to the first puzzle (puzzle 0). You should add in the answers for the remaining puzzles, following the format that we’ve given you for puzzle 0.

The expressions that you construct must only use pi and e and the following list operations:

We encourage you to try using as few operations as possible, to keep your answers elegant and efficient. However, you will get full credit for any expression that follows the rules above and produces the correct result.

Before getting started, you should run ps1pr2.py in IDLE using F5. This will make the lists pi and e available to you in the Python Shell.

Here are the puzzles:

  1. Use pi and/or e to create the list [2, 5, 9], and assign it to the variable answer0. We’ve given you the code for this puzzle.

  2. Use pi and/or e to create the list [2, 7] and assign it to the variable answer1. Your answer should follow the format that we’ve given you for problem 0. In other words, it should look like this:

    # Puzzle 1:
    # Creating the list [2, 7] from pi and e
    answer1 = 
    print(answer1)
    

    where you put the appropriate expression to the right of the assignment operator (=). Please leave a blank line between puzzles to make things more readable.

  3. Use pi and/or e to create the list [5, 4, 3], and assign it to the variable answer2. Here again, make sure to follow the correct format, and to leave a blank line between puzzles.

  4. Use pi and/or e to create the list [3, 5, 7], and assign it to the variable answer3. (Optional challenge: See if you can do this with just three list operations!)

  5. Use pi and/or e to create the list [1, 2, 3, 4, 5], and assign it to the variable answer4. (Optional challenge: See if you can do this with just three list operations!)

String puzzles
The second half of the problem focuses on strings. In particular, you will be working with the following strings:

b = 'boston'
u = 'university'
t = 'terriers'

We’ve given you these lines in ps1pr2.py, along with the answer to the first string puzzle (puzzle 5). Run the file as needed so that the strings will be available for you to experiment with in IDLE.

The expressions that you construct for the remaining puzzles must only use the above strings and the following string operations:

Here again, you will get full credit for any expression that follows the rules above and produces the correct result, but we encourage you to try using as few operations as possible.

Here are the puzzles:

  1. Use b, u, and/or t to create the string 'bossy', and assign it to the variable answer5. We’ve given you the following code for this puzzle:

    # Puzzle 5:
    # Creating the string 'bossy'
    answer5 = b[:3] + t[-1] + u[-1]
    print(answer5)
    

    Note that our answer involves 5 operations: 2 uses of indexing, 1 slice, and 2 concatenations with +. (It’s actually possible to solve this puzzle using only 3 operations. Give it a try if you have time!)

  2. Use b, u, and/or t to create the string 'universe', and assign it to the variable answer6. (Our best answer uses 3 ops.) Here again, make sure to follow the correct format, and to leave a blank line between puzzles.

  3. Use b, u, and/or t to create the string 'roster', and assign it to the variable answer7. (Our best: 5 ops.)

  4. Use b, u, and/or t to create the string 'boisterous', and assign it to the variable answer8. (Our best: 8 ops.)

  5. Use b, u, and/or t to create the string 'yesyesyes', and assign it to the variable answer9. (Our best: 4 ops.)

  6. Use b, u, and/or t to create the string 'trist', and assign it to the variable answer10. (Our best: 4 ops.)

After finishing all of the puzzles, make sure to run your ps1pr2.py file to check that the correct outputs are printed.


Submitting Your Work

You should use Apollo to submit the following files:

Warnings

  • Make sure to use these exact file names, or Apollo will not accept your files. If Apollo reports that a file does not have the correct name, you should rename the file using the name listed in the assignment or on the Apollo upload page.

  • If you make any last-minute changes to one of your Python files (e.g., adding additional comments), you should run the file in IDLE after you make the changes to ensure that it still runs correctly. Even seemingly minor changes can cause your code to become unrunnable.

  • If you submit an unrunnable file, Apollo will accept your file, but it will print a warning message. If time permits, you are strongly encouraged to fix your file and resubmit. Otherwise, your code will fail most if not all of our tests.

Here are the steps:

  1. Login to Apollo, using the link in the left-hand navigation bar. You will need to use your Kerberos user name and password.
  2. Find the appropriate problem set section on the main page and click “Upload files”.
  3. For each file that you want to submit, find the matching upload section for the file. Make sure that you use the right section for each file. You may upload any number of files at a time.
  4. Click the “Upload” button at the bottom of the page.
  5. Review the upload results. If Apollo reports any issues, return to the upload page by clicking the link at the top of the results page, and try the upload again, per Apollo’s advice.
  6. Once all of your files have been successfully uploaded, return to the upload page by clicking the link at the top of the results page. The upload page will show you when you uploaded each file, and it will give you a way to view or download the uploaded file. Click on the link for each file so that you can ensure that you submitted the correct file.

Warning

Apollo will automatically close submissions for a given file when its final deadline has passed. We will not accept any file after Apollo has disabled its upload form, so please check your submission carefully following the instructions above.

Note: If you encounter problems with Apollo, close your browser and try again. If possible, you may also want to wait an hour or two before retrying. If you are unable to submit and it is close to the deadline, email your homework before the deadline to cs111-staff@cs.bu.edu.