Professional Documents
Culture Documents
Assignment A1
Assignment A1
SECTION E
Read the instructions below carefully. The instructions must be followed. This
assignment is worth 5% of your grade. The assignment is due on Sunday 3rd of
October Midnight. No late assignments will be accepted.
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
and operations on strings, variables, Python's builtin functions, designing your own
functions, and the use of Python's input and output functions. Before you can start this
assignment, you need to know how to use a (IDLE's) text editor to edit python modules
(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 Blackboard Learn (as instructed in the first
lab.)
Each question asks you to design one function. Put all these functions (for all the
questions) 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
###################################################################
you must solve the questions below without loops, if and other branching statements,
lists unless explicitly stated otherwise in the question.
After reading each of these questions once, go to the "Testing your code" section 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 "Testing your code" and with some other examples. Thus you two should test
your functions with more example that what I provided below.
Section: Assignment 1 questions
1
2
3
6
6.
(2 points) Write a function lbs2kg(w) that returns the given weight, w, expressed in
pounds as weight in kilograms
(2 points) Write a function id_formater(fn, ln, appelation, city, year) that returns a
string of the form "appelation. ln, fn (city, year)"
(2 points) Write a personalized limerick maker in the form of a function called
limerick_maker(). This function prompts the user for the name and city of birth and
then prints a limerick that includes that name and city of birth. You can make up
your own limerick or adapt one from http://www.loonylimericks.com/. (Note,
though, that many of these are not suitable for this question since they use the
name or city of birth as part of the rhyme.)
(2 points) Write a function id_formater_display() that prompts the user for the first
name, last name, appellation, place of birth, and year of birth. The function should
then print the ID of the user using the same format as specified in question 2. (To
do that your solution must use a function call to id_formater function from question
2).
(2 points) Write a function l2loz(w) that takes a non-negative number w as input
and returns a pair of numbers (l,o) such that w = l + o/16 and l is an integer and o
is a non-negative number smaller than 16.
(2 points) Write
(2 points) write function convert_days_to_YY_MM_DD(day), which get number of
days as input and return the following string:
Year: YY, Month: MM, Day: DD
Where YY, MM and DD is the number of years, months and days converted by the
function.
Note: Assume that the year = 360 days and the month = 30 days.
(2 points) The median of a group of numbers is the number from that group that
has the property that at least half of the elements in the group are smaller or equal
10
11
to it and at least half of the elements in the group are bigger or equal to it. In a
sorted list of numbers, the median can be found in the middle of the list (well, the
middle is only well defined if the list has odd number of elements). For example,
the median of this group of numbers: 10,11,13,15,16,23,26 is 15. Median of this
group of numbers: 7,1,3 is 3. The median of this group of numbers: 2,2,5 is 2.
Write a function median3(num1,num2,num3) that prints a message for each of
the given three numbers, num1, num2 ,num3, stating if the number is a median.
See the test runs below to understand in what format should the function
median3 print its results.
(2 points) Write a function below_parabola(a,b,p,q) that returns True if the point
(p,q) in the plane (i.e., the point with x-coordinate p and y-coordinate q) is below
or on the graph of the parabola y=ax2 + b, otherwise it returns False. You may
assume that your function will be tested only with positive numbers for a (and
arbitrary numbers for b).
(2 points) Suppose that a grading scheme for a course with 3 assignments, one
midterm and one final is: each assignment is worth 5%, the midterm is worth 35%
and the final is worth 50% of the grade. Write a function
projected_grade(a1,A1,a2,A2,m,M) that predicts (i.e. computes) the final grade
percentage as follows. The student obtained a1 points out of maximum A1 for
assignment 1; a2 points out of maximum A2 for assignment 2 and m points out of
maximum M for the midterm. To compute the final course grade, assign to
assignment 3 the average percentage of assignment 1 and assignment 2 and
assign to the final exam the same the percentage as obtained on the midterm
exam. For example, if the student got 12/20 in assignment 1 and 24/24 in
assignment 2, the grade in assignment 3 should be 80% since that is the average
of the two percentanges. If the student obtained 9/12 in the midterm, the grade of
the final exam should also be 75%. Using the given grading scheme and the
predicted grades for assignment 3 and the final exam, function projected_grade
should return the predicted final percentage for the course. You can assume that
the function will be tested with reasonable values a1,A1,a2,A2,m, and M.
(2 points) Write a function called projected_grade_v2() that prompts the user to
enter 6 numbers (the meaning of these six numbers is the same as
a1,A1,a2,A2,m,M in the previous question). Compute the predicted percentage of
the final course grade with the slight modification to the grading scheme where if
the average on the midterm and the final is less than 50% the final course grade
percentage is whatever is smaller of the two grading schemes. Print a meaningful
message to a user about their predicted final grade. You may (or rather should)
use if statements in this question.
(4 points) Suppose that a cashier owes a customer some change and that the
cashier only has quarters, dimes, nickels, and pennies. Write a function that
determines the minimum number of coins that the cashier can return. In particular,
write a function change_to_coins(amount) that returns four numbers (q,d,n,p) that
represent the smallest number of coins (quarters, dimes, nickels, and pennies) that
add up to the given amount of dollars. You may assume that the input to function
change_to_coins(amount) is a float value referred to by amount representing the
number of dollars. Before doing anything else, you should convert this number
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):
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 customer is owed 1.42 dollars, thus
142 cents, the greedy strategy will first figure out how many quarters can it
give to the customer at most. In this case, it would be 5 quarters. It cannot
be 6 as that equals 1.5 and the cashier would return too much money.
Once the cashier returns 5 quarters, he/she still needs to return 17 cents.
The next biggest coin after quarter is a dime. So the greedy strategy
would try dimes next. Only one dime can fit in 17 cents, so the cashier
should next return 1 dime. Then there is 7 cents left. The next biggest coin
to consider is a nickel ... etc. It can be proved that this greedy strategy
gives an optimal solution (i.e. the smallest number of coins) for this
version of the problem. Thus for this question, you are asked to implement
this strategy to find the optimal solution.
Side note: in the Canada (and most other) coin systems, a 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.
>>>
>>> id_formater("Albert","Einstein", "Dr", "Bern", 1879)
'Dr. Einstein, Albert (Bern, 1879)'
>>>
>>> # testing Question 3
>>>
>>> limerick_maker()
Enter your name: Vida
Enter your city of birth: Mostar
Vida had funny funny hair
With tons and tons to spare
Vida's clippings made a wig
It was very big
And caused the townsfolk of Mostar to stare
>>>
>>> # testing Question 4 >>>
>>> id_formater_display()
What is your first name? Harry
What is your last name? Potter
What is your appellation? Wizard
Where were you born? Godric's Hollow
What is your year of birth? 1980
Wizard. Potter, Harry (Godric's Hollow, 1980)
>>>
>>> # testing Question 5
>>>
>>> l2loz(7.5)
(7, 8.0)
>>>
>>> l2loz(9.25)
(9, 4.0)
>>>
>>>
>>> # testing Question 6
>>>
>>> convert_days_to_YY_MM_DD(1000),
>>> Year: 2, Month: 9, Day: 10
>>>
>>> # testing Question 7
>>>
>>> median3(18, 1, 8)
18 is a median. That is False
30,30)
9,12)
60,100)
49,100)
>>>
>>> # testing Question 10
>>>
>>> projected_grade_v2()
How many points did you get in Assignment 1? 10
What was the maximum possible number of points for Assignment 1?
10
How many points did you get in Assignment 2? 9
What was the maximum possible number of points for Assignment 2?
10
How many points did you get on the midterm? 49
What was the maximum possible number of points for the midterm?
100
Your predicted final grade is 49.0 %
>>> projected_grade_v2()
Assignment 1?
Assignment 2?
the midterm?
Assignment 1?
Assignment 2?
the midterm?