CS 111
Summer 1 2020

# Problem Set 1

due by 9:00 p.m. EDT on Thursday, May 21, 2020

## 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 Gradescope, 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
```

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 line 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 assign the result of the computation to an appropriately named variable.

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:

• There are multiple ways to compute a given integer. For example, here is an alternative approach for 0:

`zero = 4//4 - 4//4`

• We encourage you to try to come up with each of the expressions on your own. However, the real point of this problem is getting comfortable with Python, so if you have trouble coming up with an expression for a given number, feel free to ask a classmate or one of the course staff members, or to consult information about the four fours challenge that is available online. However, please do not post your expressions on Piazza for everyone to see.

### 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!

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:

• list indexing (e.g., `pi[0]`)
• list slicing (e.g., `e[1:]` or `pi[2:4]`)
• skip-slicing (e.g., `pi[6:4:-1]`)
• list concatenation (e.g., `pi[:1] + e[1:]`. Note that you may not use `+` to add values numerically.)
• list construction (e.g., `[e[2]]` or `[e[2], e[0]]`)

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
```

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:

• string indexing (e.g., `b[0]`)
• string slicing (e.g., `u[1:]` or `t[2:4]`)
• skip-slicing (e.g., `u[6:4:-1]`)
• string concatenation (e.g., `b + u`)
• string repetition (e.g., `5*b` or `3*u[-1]`)

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]
```

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.

You should use Gradesope to submit the following files:

• your modified `ps1pr1.py` file containing your solutions for Problem 1
• your modified `ps1pr2.py` file containing your solutions for Problem 2

Warnings

• Make sure to use these exact file names, or Gradescope will not accept your files. If Gradescope 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, Gradescope will accept your file, but it will not be able to auto-grade it. 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.

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`.