CS108
Spring 2024

Assignment 1: Introduction to Python

due by 9:00 p.m. EST on Tuesday 1/23/24

Preliminaries

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

For each problem in this problem set, we will be writing or evaluating some Python code. You are encouraged to use the Spyder IDE which will be discussed/presented in class, but you are welcome to use another IDE if you choose.

If you have questions while working on this assignment, please post them on Piazza! This is the best way to get a quick response from your classmates and the course staff.

Programming Guidelines

  • Refer to the class Coding Standards for important style guidelines. The grader will be awarding/deducting points for writing code that comforms to these standards.

  • Every program file must begin with a descriptive header comment that includes your name, username/BU email, and a brief description of the work contained in the file.

  • Every function must include a descriptive docstring that explains what the function does and identifies/defines each of the parameters to the function.

  • Your functions must have the exact names specified below, or we won’t be able to test them. Note in particular that the case of the letters matters (all of them should be lowercase), and that some of the names include an underscore character (_).

  • Make sure that your functions return the specified value, rather than printing it. None of these functions should use a print statement.

  • If a function takes more than one input, you must keep the inputs in the order that we have specified.

  • You should not use any Python features that we have not discussed in class or read about in the textbook.

  • Your functions do not need to handle bad inputs – inputs with a type or value that doesn’t correspond to the description of the inputs provided in the problem.

  • You must test your work before you submit it You can prove to yourself whether it works correctly – or not – and make corrections before submission. If you need help testing your code, please ask the course staff!

  • Do not submit work with syntax errors. Syntax errors will cause the Gradescope autograder to fail, resulting in a grade of 0.

Warnings: Individual Work and Academic Conduct!!

  • This is an individual assignment. You may discuss the problem statement/requirements, Python syntax, test cases, and error messages with your classmates. However, each student must write their own code without copying or referring to other student’s work.

  • It is strictly forbidden to use any code that you find from online websites including but not limited to as CourseHero, Chegg, or any other sites that publish homework solutions.

  • It is strictly forbidden to use any generative AI (e.g., ChatGPT or any similar tools**) to write solutions for for any assignment.

Students who submit work that is not authentically their own individual work will earn a grade of 0 on this assignment and a reprimand from the office of the Dean.

If you have questions while working on this assignment, please post them on Piazza! This is the best way to get a quick response from your classmates and the course staff.


Task 1: The Four Fours Challange

40 points; individual-only

To practice arithmetic expressions in Python, you will come up with four arithmetic expressions that will produce the integer values 1, 2, 3, and 4.

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 the starter-code file a01_four_fours.py. Save this file to your computer. Open it in Spyder’s editor window using the File/Open ... menu choice.

We have given you an example expression for computing the value 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.

Notes:


Task 2: Making Change from a Dollar

40 points; individual-only

Begin by downloading the starter-code file a01_change.py. Save this file to your computer. Open it in Spyder’s editor window using the File/Open ... menu choice.

Write a program that, given a price in cents that is less than a dollar (i.e., 100 cents), determines the method of giving change that uses the fewest coins. The program will not take input from the user, but rather it will begin with a hard-coded value for the price, i.e.:

price = 67

The result of the program should be given in the format below: pennies (1 cent) first, then nickels (5 cents), dimes (10 cents), and quarters (25 cents), each on a separate line):

Here’s an example of the output, given an intial price of 67 cents:

The price of your item is 67 cents, and your change is 33 cents.
Here's the change that uses the fewest coins:

    pennies: 3
    nickels: 1
    dimes: 0
    quarters: 1

Note: this program can be done with only the arithmetic operators: + - // * %. In addition, you must create variables (pennies, nickels, dimes, and quarters) to hold the values you have caculated.

You may include any additional variables you choose.

Your finished program should print out the results, similar to the example above.

There is no need for if statements or anything else we have not discussed yet.

Designing and Planning Your Solution: Pseudo code and Comments

The most successful way to write this kind of program is to begin by thinking about the algorithm and outlining the steps required in the order they need to be performed. Pseudo-code is a way to write the steps of your algorithm without worrying about specific Python syntax.

To begin, write out pseudo code as comments (beginning with #) for each step you need to take, one per line. After you have the steps logically worked out as pseudo code, go back and write in the python code for each step. Do not delete the pseudo code! This is your program documentation and should remain in the finished product.

Practicing Step-wise Refinement

It is crucial that you get into the habit of writing a small amount of code and testing it thoroughly. You should work through this assignment using step-wise refinement as follows. For each step below, you should write the code, and then test your program to verify that each step works correctly.

a. Write a program will begin with the hard-coded price, find the change (in cents), store it in a variable, and print it out.

b. Continue your program by finding the number of quarters required for the change, and storing this amount in a variable. Hint: use integer division. Print out the number of quarters.

c. Continue by finding the number of dimes. Hint: how much change is left after you found the number of quarters?

d. Continue to find the number of nickels and pennies.

e. Revise/reorder your print statements to obtain the specified output (above).

Testing your program

Once you believe that you have a working program (i.e., no syntax erros that prevent it from running), you must test it thoroughly for correctness. That is, you must try different values for the price, by changing the single line that assigns a value to the variable price.

For example, try changing the price to 1; the result should be 99 cents of change. Try other permutation to be certain that your program will work for ANY value of price less than 100. As you test, you might discover logical/arithmetic errors in your work. This is expected! Use this as an opportunity to revise your work to achieve a correct solution.


Submitting Your Work

20 points; will be assigned by code review

Log in to GradeScope to submit your work.

Be sure to name your files correctly!

Under the heading for Assignment 1, attach each of the 2 required files to your submission.

When you upload the files, the autograder will test your program.

Notes: