Assignment 2:: Due 8am On Mon, Sept 30, 2024
Assignment 2:: Due 8am On Mon, Sept 30, 2024
Summary of Instructions
Note Read the instructions carefully and follow them exactly
Assignment Weight As outlined in the syllabus, this assignment is worth 2.5% of your final grade
Due Date This assignment is due 8am on Monday, Sept 30, 2024
As outlined in the syllabus, late submissions will not be accepted
Important Any files with syntax errors automatically be excluded from grading. Be sure to
test your code before you submit it
For all functions, make sure you’ve written good docstrings that include type
contract, function description and the preconditions if any. That includes functions
you write both in Part 1 and Part 2.
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 missing assignments on page 14.
The goal of this assignment is to learn and practice the concepts covered thus far: function design, function calls,
branching (i.e. if statements), strings. You can make multiple submissions, but only the last submission before the
deadline will be graded. What needs to be submitted is explained next.
Your grade will partially be determined by automatic (unit) tests that will test your functions. All the specified
requirements below are mandatory (including function names). Any requirement that is specified and not met may/will
result in deduction of points.
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. For this assignment you must
submit 5 files as described below. For each missing file there will be a grade deduction:
The assignment has two parts. Each part explains what needs to be submitted. Put all those required documents into
a folder called a2_xxxxxx where you changed xxxxxx to your student number, zip that folder (do not use rar compression
format) and submit it as explained in Lab 1. In particular, the folder must have the following 5 files:
– for Part 1: a2_part1_xxxxxx.py, a2_part1_xxxxxx.txt
– for Part 2: a2_part2_xxxxxx.py a2_part2_xxxxxx.txt and
– references-YOUR-FULL-NAME.txt
Both of your programs/parts must run without syntax errors. In particular, when grading your assignment, TAs will
first open your file a2_part1_xxxxxx.py with IDLE and press Run Module. If pressing Run Module causes any syntax
error, the grade for Part 1 becomes zero. The same applies to Part 2, when they open and run file a2_part2_xxxxxx.py.
Furthermore, for each of the functions (in Part 1 and Part 2), I have provided one or more tests to test your functions
with. For example, you should test function high_school_quiz from Part 1 by making a call in Python shell with
high_school_quiz(3,2,15) 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.
Section 3, contains tests for Part 1. Tests for Part 2 are provided after each question in Part 2. To determine your grade,
your functions will be tested both with examples provided in Part 2 and Section 3 and with some other examples. Thus
you too should test your functions with more example than what I provided in Part 2 and Section 3.
Each function has to be documented with docstrings.
Global variables are not allowed. If you do not know what that means, for now, interpret this to mean that inside of
your functions you can only use variables that are created in that function. For example, this is not allowed, since variable
x is not a parameter of function a_times(a) nor is it a variable created in function a_times(a). It is a global variable
created outside of all functions.
1
def a_times(a):
result=x*a
return result
• Source of the copied code: name of the person or the place on the internet/book where you found it.
While you may not get points for copied parts of the question, you will not be in the position of being accused of
plagiarism. Any student caught in plagiarism will receive zero for the whole assignment and will be reported to the dean.
Showing/giving any part of your assignment code to a friend also constitutes plagiarism and the same penalties will
apply. If you have nothing to declare/reference, then just write a sentence stating that and put your first and last name
under that sentence in your references-YOUR-FULL-NAME.txt file.
Not including references-YOUR-FULL-NAME.txt file, will be taken as you declaring that all the code in the assignment
was written by you. Recall though that not submitting that file, comes with a grade penalty.
elementary_school_quiz: This function has two parameters, namely an integer flag that takes only values 0 or 1 and
an integer n that takes only values 1 or 2. If flag is 0, elementary_school_quiz helps practice the inverse of
exponentiation i.e. logarithm. But if flag is 1, elementary_school_quiz helps practice exponentiation. In both
cases only questins with base 2 will be asked. The function, elementary_school_quiz then generates n math
problems that a pupil must answer in turn. For each question, it generates one random number between 0 and 10
inclusively (check out python’s random module to see if there’s a useful function in there) and asks the pupil for the
2
answer to the math problem with the random number. Specifically, if flag is 0 and random number generated is,
for example, 5 the question asked should be: “2 to what is 32 i.e. what is the result of log2 (32)?”. (The reason 32
is picked is because 25 = 32. See section 3 for more examples. If flag is 1 and random number generated is, for
example, 6 the question asked should be: “What is the result of 26 ?”
elementary_school_quiz then prompts the pupil for the answer, and checks if her answer is correct. At the end of
n questions, elementary_school_quiz returns the number of questions answered correctly.
high_school_quiz: This function has three parameters representing three real numbers for the coefficients of the quadratic
equation ax2 + bx + c = 0. The function displays/prints the equation frist and then prints its solutions. The function
must display correct and meaningful solutions given any three real numbers for coefficients a, b and c. See examples
in Section 3 to understand what that means. Please consider the examples to be a part of the function/program
specifications that must be followed.
Do not use Python’s complex numbers class to solve this problem.
1.2 The User Interaction i.e. the main part of the program
Now that you have the two functions that perform the core functionality, you want to make it more user friendly for the
pupils (after all, alas, the pupil may not know how to write code and call functions in Python shell). In the main part of
your program, write your code in specified places. You code must follow the behaviour indicated in the example runs in
Section 3.
For example, for elementary school pupils, called Mia, you will first ask her whether she would like to practice
exponentiation or its inverse. Then ask her how many practice questions she’d like (if she says 0, then your code should
not ask her to solve any math questions). Using her responses, call the elementary_school_quiz function with the
appropriate values. When it returns the number of correct answers, display a message to the pupil:
• If she go all questions correctly display on screen: Congratulations Mia! You’ll probably get an A tomorrow.
Good bye Mia!
• If she got half of them correctly display on screen: You did ok Mia, but I know you can do better. Good
bye Mia!
• If she did all incorrectly, display on screen: I think you need some more practice Mia. Good bye Mia!
For high school pupils, in the main part of your program in the specified places, you need to write some code that asks
the pupil for the coefficients a, b and c. Then you need to make the call to high_school_quiz to display the solutions.
After that your program should ask the pupil if they would like another quadratic equation solved. If the pupil says
anything but yes the program terminates by printing a good bye message as in the examples. Otherwise, as long as pupil
answers yes (and any form of typing yes should be acceptable, including with lots of white space before and after, and
with capital letters and lower case letter etc), she should be asked for the coefficients again and the resulting new quadratic
equation should be solved. Since you have not seen while loops yet, I provided the code for that in a2_part1_xxxxxx.py.
The rest of the specifications for your program in Part 1 can be inferred from examples in Section 3. You will notice
that the program is required to display greetings surrounded with stars. You may write a separate function for that.
3
>>> min_enclosing_rectangle(1,1,1)
(0, 0)
>>> min_enclosing_rectangle(4.5, 10, 2)
(5.5, -2.5)
>>> min_enclosing_rectangle(-1, 10, 2)
>>> min_enclosing_rectangle(500, 1000, 2000)
(500, 1500)
>>> vote_percentage('yes yes yes yes yes abstained abstained yes yes yes yes')
1.0
>>> vote_percentage('yes,yes, no, yes, no, yes, abstained, yes, yes,no')
0.6666666666666666
>>> vote_percentage('abstained no abstained yes no yes no yes yes yes no')
0.5555555555555556
>>> vote_percentage('no yes no no no, yes yes yes no')
0.4444444444444444
>>> l2lo(7.5)
(7, 8.0)
>>>
>>> l2lo(9.25)
(9, 4.0)
>>>
4
3 Testing your code in Part 1
Here is how you should test your two functions from Part 1 in Python shell.
>>> elementary_school_quiz(0,2)
Question 1:
2 to what is 256 i.e. what is the result of log_2 (256)? 7
Question 2:
2 to what is 8 i.e. what is the result of log_2 (8)? 3
1
>>> elementary_school_quiz(0,1)
Question 1:
2 to what is 64 i.e. what is the result of log_2 (64)? 5
0
>>> elementary_school_quiz(0,1)
Question 1:
2 to what is 16 i.e. what is the result of log_2 (16)? 4
1
>>> elementary_school_quiz(1,1)
Question 1:
What is the result of 2^10? 512
0
>>> elementary_school_quiz(1,1)
Question 1:
What is the result of 2^1? 2
1
>>> elementary_school_quiz(1,2)
Question 1:
What is the result of 2^9? 512
Question 2:
What is the result of 2^8? 256
2
>>>
>>> high_school_quiz(1,3,1)
The quadratic equation 1·x^2 + 3·x + 1 = 0
has the following real roots:
-0.3819660112501051 and -2.618033988749895
>>>
>>>
>>> high_school_quiz(3,2,15)
The quadratic equation 3·x^2 + 2·x + 15 = 0
has the following two complex roots:
-0.3333333333333333 + i 2.2110831935702664
and
-0.3333333333333333 - i 2.2110831935702664
>>>
>>>
>>> high_school_quiz(3,2,-15)
The quadratic equation 3·x^2 + 2·x + -15 = 0
has the following real roots:
1.9274433277084226 and -2.5941099943750894
>>>
>>>
>>> high_school_quiz(1,2,1)
The quadratic equation 1·x^2 + 2·x + 1 = 0
has only one solution, a real root:
-1.0
>>>
>>>
>>> high_school_quiz(0,2,4)
The linear equation 2·x + 4 = 0
has the following root/solution: -2.0
>>>
>>>
>>> high_school_quiz(0,0,0)
The quadratic equation 0·x + 0 = 0
is satisfied for all numbers x
>>>
>>>
>>> high_school_quiz(0,0,10)
The quadratic equation 0·x + 10 = 0
is satisfied for no number x
Here is what pressing Run on your program (Part 1) should look like:
5
*******************************************
* *
* __Welcome to my math quiz-generator__ *
* *
*******************************************
****************************************************************************
* *
* __Vida, welcome to my quiz-generator for elementary school students.__ *
* *
****************************************************************************
*******************************************
* *
* __Welcome to my math quiz-generator__ *
* *
*******************************************
>>>
*******************************************
* *
* __Welcome to my math quiz-generator__ *
* *
*******************************************
************************************************************************
* *
* __quadratic equation, a·x^2 + b·x + c= 0, solver for Arya Stark__ *
* *
6
************************************************************************
*******************************************
* *
* __Welcome to my math quiz-generator__ *
* *
*******************************************
******************************************************************************
* *
* __ Ji-Ah, welcome to my quiz-generator for elementary school students.__ *
* *
******************************************************************************
7
*******************************************
* *
* __Welcome to my math quiz-generator__ *
* *
*******************************************
*********************************************************************************
* *
* __Hippolyta, welcome to my quiz-generator for elementary school students.__ *
* *
*********************************************************************************
*******************************************
* *
* __Welcome to my math quiz-generator__ *
* *
*******************************************
****************************************************************************
* *
* __Leti, welcome to my quiz-generator for elementary school students.__ *
* *
****************************************************************************
*******************************************
* *
* __Welcome to my math quiz-generator__ *
* *
*******************************************
****************************************************************************
8
* *
* __Wolf, welcome to my quiz-generator for elementary school students.__ *
* *
****************************************************************************