首页 > > 详细

辅导 Assignment 2讲解 留学生Python程序

Assignment 2: due 11:59 pm on Saturday, October 5, 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  11:59 on Saturday, October 5, 2024

Important

As outlined in the syllabus, late submissions will not be accepted

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

I suggest you do part 2 first and then do part 1.

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.

def  a_times(a):

result=x*a

return  result

x=float(input("Give me  a  number:  "))

print(a_times(10))

About references-YOUR-FULL-NAME . txt le:

The file must be a plain text file.  The file must contain references to any code you used that you did not write yourself, including any code you got from a friend, internet, AI engines like chatGPT, social media/forums (including Stack Overflow and discord) or any other source or a person.   The only exclusion from that rule is the code that we did in class, the code done as part the lab work, or the code in your textbook.  So here is what needs to be written in that file.  For every question where you used code from somebody else:

  Write the question number

•  Copy-paste all parts of the code that were written by somebody else.  That includes the code you found/were-given and that you then slightly modified.

•  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.

1    Part 1: Math quiz-generator / equation-solver - 40 points

You would like to help in your community center teach elementary and high school students math.  To reach as many kids as possible, you decided to write some software that can automate some tasks for you.  As starting point, you decided to make an automatic quiz generator and grader for elementary school pupils and a quadratic equation solver for highschool pupils – but unlike the solver you did in your Lab 2, this quadratic equation solver needs to handle all possible cases including those when the given quadratic equation has complex numbers for solutions.  In case of elementary school pupils, your understanding is that they already know well addition and multiplication but that they struggle with exponentiation and its inverse.

For this part, I provided you with starter code in file called a2_part1_xxxxxx.py.  Begin by replacing xxxxxx in file name with your student number.  Then open the file.  Your solution (code) for this part must go into that file in clearly indicated spaces.  You are not allowed to delete or comment-out any parts of the provided code except for the keywords pass.

For this part you need to submit two files:  a2_part1_xxxxxx.py and a2_part1_xxxxxx.txt

a2_part1_xxxxxx.py needs to contain your program for Part 1 as explained above and a2_part1_xxxxxx.txt needs to  contain  the  proof that  you  tested  your  two  core  functions  from  this  part,  namely  elementary_school_quiz  and high_school_quiz

1.1    The Core Functions

Your solution in a2_part1_xxxxxx.py must have two functions called: elementary_school_quiz and high_school_quiz.

You should design and test these functions first before moving onto the main part of the program. Here are specifications for the two functions:

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 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 ofthe 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.

2    Part 2: A Library of Functions

For this part of the assignment, you are required to write and test four functions  (as you did in Assignment  1).   You need to save all functions in part2_xxxxxx.py where you replace xxxxxx by your student number.  You need to test your functions (like you did in Assignment 1) and copy/paste your tests in part2_xxxxxx.txt.  Thus, for this part you need to submit two files:  a2_part2_xxxxxx.py and a2_part2_xxxxxx.txt

2.1    min_enclosing_rectangle(radius,  x,  y) - 5 points

Computing a smallest (axis-aligned) rectangle that encloses a set of objects in the plane is a very common computational problem arising in graphics and thus game development too. Write a function, called min_enclosing_rectangle, that has 3 input parameters.  The first is a number representing a radius of a circle and the next two are two numbers representing the x- and y-coordinates of its center.  Consider the smallest axis-aligned rectangle that contains that circle.  The function should  return  the x-  and  y-coordinates  of the  bottom-left  corner  of that  rectangle.   If  radius is  a  negative  number, min_enclosing_rectangle should return None.

>>> 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)

2.2    vote_percentage  (results) - 5 points

Write a function called vote_percentage that takes a string as input.   The function has one input parameter,  called  results. Your function should count the number of substrings ’yes’ in the string results and the number of substrings ’no’ in the string results, and it should return the percentage of ’yes’ (among all ’yes’ and ’no’).  (You may assume that string  results has at least one yes or no and that the only words present are yes, no and/or abstained).

Hint: you may use count method from Python’s str module/library.

>>>  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

2.3    vote() - 5 points

If there is a vote at a meeting, there are several possible outcomes based on the number of yes and no votes  (abstains are not counted).  If all the votes are yes, then the proposal passes  "unanimously", if at least 2/3 of the votes are yes, then the proposal passes with "super majority", if at least 1/2 of the votes are yes, then the proposal passes by "simple majority", and otherwise it fails. Write a function called vote that asks a user to enter all yes-s and no-s and abstained-s and then press enter.   The function then prints the outcome of the vote.   You solution must involve making a call to function vote__percentage (You may assume that the user will enter at least one yes or no and that the only words present are yes, no and/or abstained)

>>>  vote()

Enter  the  yes,  no,  abstained  votes  one  by  one  and  then  press  enter:

yes  yes  yes  yes  yes  abstained  abstained  yes  yes  yes  yes

proposal  passes  unanimously >>>  vote()

Enter  the  yes,  no,  abstained  votes  one  by  one  and  then  press  enter:

yes,yes,  no,   yes,  no,  yes,  abstained,  yes,   yes,no

proposal  passes  with  super majority >>>  vote()

Enter  the  yes,  no,  abstained  votes  one  by  one  and  then  press  enter:

abstained  no  abstained  yes  no  yes  no  yes  yes  yes  no

proposal  passes  with  simple majority >>>  vote()

Enter  the  yes,  no,  abstain  votes  one  by  one  and  then  press  enter:

no  yes  no  no  no,  yes  yes  yes  no proposal  fails

2.4       l2lo(w) - 5 points

Write a function called l2lo(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.  Note that the solution l and o are unique.

>>>  l2lo(7.5)

(7,  8.0)

>>>

>>>  l2lo(9.25)

(9,  4.0)

>>>



联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!