Assignment 1: 2.5% Friday, 26th of January at 8AM
Assignment 1: 2.5% Friday, 26th of January at 8AM
Read the instructions below carefully. The instructions must be followed. This assignment is
worth 2.5% of your grade. The assignment is due on Friday, 26th of January at 8AM. No late
assignments will be accepted.
This is an individual assignment. Please review the Plagiarism and Academic Integrity policy
presented in the first class, i.e. read in detail pages 16-20 of course outline (i.e. slides of
Lecture 1). You can find that file on Brightspace under Lecture 1. While at it, also review
Course Policy on missed assignments on pages 14.
The goal of this assignment is to learn and practice (via programming) the concepts that we
have learned so far: numbers, algebraic expressions, boolean expressions, strings, operations
on strings, type conversion, variables, use of Python's builtin functions including input and
output functions, designing your own functions, documenting your own functions via
docstrings, and testing your functions. Before you can start this assignment, you need to know
how to use a (IDLE's) text editor to edit python modules (i.e. files) as well as how to use
python shell interactively to test your code. If you have no idea how to do these things
watching video of the 3rd lecture, for example, will help. Submit your assignment by the
deadline via Brightspace (as instructed and practiced in the first lab.) You can make multiple
submissions, but only the last submission before the deadline will be graded. What needs to
be submitted is explained next.
The assignment has 12 programming questions (in Section 1 below). Each question asks you to
design one function. Put all these functions (for all the questions below) in ONE file only,
called a1_xxxxxx.py (where xxxxxx is replaced with your student number). Within this file,
a1_xxxxxx.py, separate your answers (i.e. code) to each question with a comment that looks
like this:
###################################################################
# Question X
###################################################################
To have an idea on what this file a1_xxxxxx.py should look like, I included with this
assignment a solution to an nonexistent assignment. You can view this mockup solution by
opening the included file called a1_mockup_assignment_solution.py
In addition to a1_xxxxxx.py you must submit a separate file called a1_xxxxxx.txt. What
should be in that file is explained below. Thus for assignment 1 you have to SUBMIT TWO
FILES:
• a1_xxxxxx.py and
• a1_xxxxxx.txt.
as instructed in lab 1. Submit your assignment by the deadline via Brightspace (as instructed
in the first lab.)
Your program must run without syntax errors. In particular, when grading your assignment,
TAs will first open your file a1_xxxxxx.py with IDLE and press Run Module. If pressing Run
Module causes any syntax error, the grade for the whole assignment will be zero.
Furthermore, for each of the functions below, I have provided one or two tests to test your
functions with. For example, you should test question 1 by making function call f_to_k(90)
in the Python shell. To obtain a partial mark your function may not necessarily give the
correct answer on these tests. But if your function gives any kind of python error when run on
the tests provided below, that question will be marked with zero points.
After reading each of these questions once, go to the Section 2: “Testing your code" below
and see what the output of your function should give. In that section, you can find a couple of
function calls and the required results for each question. Studying these example function
calls will help you a lot to understand what each individual question requires, or rather what
its function should do.
To determine your grade, your functions will be tested both with examples provided in
Section 2: "Testing your code" and with some other examples. Thus you too should test your
functions with more example than what I provided in Section 2.
Each function must be documented with docstrings (as will be explained upcoming lectures).
In particular, each function has to have docstrings that specify:
- type contract
- description about what the function does (while mentioning parameter names)
- preconditions, if any
The purpose of this assignment is to practice concepts that you have seen in the first 2.5
weeks of class. Thus this assignment does not require use of loops, if and other branching
statements, lists ... etc, (except Question 10 if you want to be creative). Thus you must
solve the questions below without loops, if and other branching statements, lists etc.
Question that break this rule will receive zero since they suggest that the required
understanding of the material covered in first 2.5 weeks is not attained.
Global variables are not allowed. If you do not know what that means, for now, interpret this
to mean that inside of your file a1_xxxxxx.py variables can only be created (ie. assigned
value) inside of the bodies of your functions.
To avoid confusion, unless otherwise specified in the questions here is what you can use in
this assignment:
- comparison operators: <,<=, ==, !=, >, >=
- Boolean operators: and, or, not
- arithmetic operators: +, -, *, /, **, %, //
- the following Python built in functions: print, input, round, len, int, float, str
- string operators: +, *
- any function from the math module (recall import math, dir(math), and then you can call
help on any function in math module. eg. help(math.sqrt) )
- anything from Turtle module
- keywords: def, return
10. (2 points) Write a function draw_rocket() that draws, with Turtle graphics, a rocket or
a space ship as in the image below. Your drawings should not be much simpler than what
is in images below but it can be more complex. In this questions you can use loops and/or
if statements if you want to draw something more complex.
11. (2 points) Write a function cad_cashier(price,payment) that takes two real non-negative
numbers with two decimal places as input, where payment>=price and where the second
decimal in payment is 0 or 5. They represent a price and payment in Canadian dollars.
The function should return a real number with 2 decimal places representing the change
the customer should get in Canadian dollars. Recall that in Canada, while the prices are
expressed in pennies, the change is based on rounding to the closest 5 cents. See the
examples in Section 2 for clarification and examples on how your function must behave.
12. (5 points) Suppose that a cashier in Canada owes a customer some change and that the
cashier only has coins ie. toonies, loonies, quarters, dimes, and nickels. Write a function
that determines the minimum number of coins that the cashier can return. In particular,
write a function min_CAD_coins(price,payment) that returns five numbers (t,l,q,d,n)
that represent the smallest number of coins (toonies, loonies, quarters, dimes, and
nickels) that add up to the amount owed to the customer (here price and payment are
defined as in the previous question). You program must first call cad_cashier function,
from question 11, to determine the amount of change that needs to be returned.
Then before doing anything else, you may want to convert this amount entirely to cents
(that should be of type int). Once you have the total number of cents here are some
hints on how to find the minimum number of coins.
Hints for your solution (algorithm) for question 12:
To find the minimum number of coins the, so called, greedy strategy (i.e. greedy algorithm)
works for this problem. The greedy strategy tries the maximum possible number of the biggest-
valued coins first, and then the 2nd biggest and so on. For example, if price is $14.22 and
payment $20, the customer is owed $5.80 (after rounding to closest 5 cents), thus 580 cents,
the greedy strategy will first figure the maximum number of toonies it can give to the customer.
In this case, it would be 2 toonies. It cannot be 3 toonies as that equals $6 and the cashier
would return too much money. Once the cashier returns 2 toonies, he/she still needs to return
180 cents. The next biggest coin after toonie is a loonie. So the greedy strategy would try
loonies next. Only 1 loonie can fit in 180 cents, so the cashier should next return 1 loonie. Then
there is 80 cents left. The next biggest coin to consider is a quarter … and so on … ending with
nickels. (For this example the function should return (2,1,3,0,1) ). Thus for this question, you
are asked to implement this strategy to find the optimal solution. See section 2 for more
examples.
Side note: in the Canada (and most other) coin systems, the greedy algorithm of picking the
largest denomination of coin which is not greater than the remaining amount to be made will
always produce the optimal result (i.e. give the smallest number of coins). This is not
automatically the case, though: if the coin denominations were 1, 3 and 4 cents then to make
6 cents, the greedy algorithm would choose three coins: one 4-cent coin and two 1-cent coins
whereas the optimal solution is two 3-cent coins.
We would like to see evidence that you have tested your functions in python shell, like we did
in class. Do this by opening your assignment solution, i.e. opening a1_xxxxxx.py, with
IDLE and then test each of your functions individually. Then copy and paste the python shell
output into a text file called a1_xxxxxx.txt The contents of a1_xxxxxx.txt must have
something like this in it:
>>> # testing Question 1
>>>
>>>
>>> f_to_k(90)
305.3722222222222
>>>
>>> f_to_k(-457.87)
1.0
>>>
>>> # testing Question 2
>>>
>>>
>>> poem_generator()
Enter your name: Vida
Enter your city of birth: Mostar