CS 124
Fall 2023

Problem Set 1

due by 10:00 p.m. EDT on Friday, September 1, 2021

suggested self-deadline of Wednesday, August 30, 2023

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 or post them on Piazza.

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 VS Code Python development environment.


Problem 1: The four fours challenge

50 points; pair or group optional

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 5 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 VS Code 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 5 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 Python Interactive Window in VS Code. To run it, right-click inside your code file, and select “Run Current File in Python Interactive Window”.

Doing so will take you to a separate window for the Python Interactive Console, 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
five = 5

Notes:



Problem 2: Indexing and slicing puzzles

50 points; pair or group optional

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

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 VS Code using the Run Current File in Interactive Window option. This will make the lists pi and e available to you in the interactive window so that you can experiment with them.

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 =
    

    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:

c = 'clark'
u = 'university'
t = 'cougars'

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 VS Code.

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 c, u, and/or t to create the string 'classy', and assign it to the variable answer5. We’ve given you the following code for this puzzle:

    # Puzzle 5:
    # Creating the string 'classy'
    answer5 = c[:3] + t[-1] + t[-1] + u[-1]
    
  2. Use c, u, and/or t to create the string 'nonononono', and assign it to the variable answer6. Here again, make sure to follow the correct format, and to leave a blank line between puzzles.

  3. Use c, u, and/or t to create the string 'classover', and assign it to the variable answer7.

  4. Use c, u, and/or t to create the string 'serenity', and assign it to the variable answer8.

  5. Use c, u, and/or t to create the string 'stone', and assign it to the variable answer9.

  6. Use c, u, and/or t to create the string 'invite', and assign it to the variable answer10.

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

The Gradescope submission system will be activated later in the week. We will demonstrate using it during the scheduled lab time.

You should use Gradescope to submit the following files:

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 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 VS Code 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 your instructor.