程序代写代做 go Assignment 1 COMP 208 Winter 2020

Assignment 1 COMP 208 Winter 2020
posted: Wednesday, January 22, 2020
due: Wednesday, February 5, 2020 at 23:59
Primary Learning Objectives
By the end of this assignment, students should be able to…
• Use the Thonny program to write and run Python code.
• Perform basic mathematical calculations.
• Get input from the user and handle cases of invalid user input.
• Translate mathematical formulas and sequences into Python.
• Use the following basic Python language constructs: variables, functions, if statements and basic for loops.
Submission instructions
• This file should have been accompanied by a .zip file called Assignment1.zip. Unzip this file to produce a directory called Assignment1 containing four Python code files. You will write the solutions to this assignment in these four files.
• When ready to submit, zip up the Assignment1 folder and submit the resulting Assign- ment1.zip file to myCourses. Before zipping, make sure that the folder is still named As- signment1 and that the four Python code files inside are still named as follows:
– gaussian.py – artwork.py – sphere.py
– vink.py
Any deviation from these requirements will result in deduction of points.
• You can submit multiple times on myCourses, so don’t worry if you realize that your current submission contains an error.
• Submit early, submit often! (Computer crashes do occur, and myCourses may be overloaded during rush hours.)
1

Coding instructions
• You must include your name and McGill ID number at the top of each Python code file that you implement and submit. By doing so, you are certifying that the code file is entirely your own, and represents the result of your sole effort.
• You are expected to put comments in each file, on average 1 comment for every 4-5 lines, to explain what the code is doing. Failure to do so will result in deduction of points.
• You are expected to use descriptive names for variables whenever possible. Do not use variable names like x, y or z (unless you are dealing with a mathematical function like f(x)). Instead, use names like user input, sum of numbers, or average value. Failure to give your variables descriptive names will result in deduction of points.
• Some questions will ask you to define a function of a given name. The function you define must have the same name as given. Further, every question will have examples to show the output that your program is required to produce. Make sure that the output of your program exactly matches the output of the examples. (Input values are highlighted in gray, as they should be input by the user.) Failure to exactly match function names and output will result in deduction of points.
• If you have prior programming knowledge, you may know how to do a question using more advanced concepts. Although it may be more efficient to use such advanced concepts, it defeats the purpose of the assignment as we are testing on specific language constructs. Therefore, please only use concepts discussed in class up to and including January 22 (the day Assignment 1 was posted). Solutions that use Python concepts seen after January 22 (such as while loops, lists, tuples or any third-party libraries) will be penalized.
Policies
• Late assignments will be accepted up to 2 days (48 hours) after the due date and will be penalized by 10 points per day. Note that submitting one minute late is the same as submitting 23 hours late. We will deduct 10 points for any student who submits or resubmits after the due date irrespective of the reason, be it wrong file submitted, wrong file format submitted or any other reason. This policy will hold regardless of whether or not the student can provide proof that the assignment was indeed “done” on time.
• If your program does not work at all, e.g., gives an error and does not produce any output, zero points will be given for that question. If your program executes without errors but produces incorrect output, partial marks may be awarded based on the correctness of the code.
• If anything is unclear, it is up to you to seek clarification by either directly asking a TA during office hours or making a post on the myCourses discussion board.
• Work submitted in this course must represent your own efforts. Assignments must be done individually; you must not work in groups. Do not ask friends or tutors to do this assignment for you. You must not copy any other person’s work in any manner (electronically or other- wise), nor give a copy of your work to any other person. Code similarity detection software will be run on all assignments.
2

Question 1 [10 points]
The Gaussian function is one of the most widely used functions in science and technology. The two-dimensional Gaussian function can be expressed as follows:
(x − xo)2 (y − yo)2 f(x,y) = Aexp(−( 2σX2 + 2σY2 ))
where the parameters A, xo, yo, σX , σY are real numbers. Write a function gaussian 2D that takes in parameters x, y, A, xO, yO, sigmaX, sigmaY , evaluates the Gaussian with those parameters, and returns the result. Then, outside the function, ask the user to enter in values for each of the seven parameters, call the function with those parameters, and print the result to the screen.
Note that exp is the exponential function (ex) and can be expressed in Python using the math module function math.exp(x).
Filename
You must write this program in a file called gaussian.py.
User input
• Seven numerical values, one for each parameter: x, y, A, xO, yO, sigmaX, sigmaY . (You can assume the user will always type in a number.)
Examples (as executed in Thonny)
Example 1:
>>> %Run gaussian.py
Enter value for x:
Enter value for y:
Enter value for A:
Enter value for xO:
Enter value for yO:
Enter value for sigmaX:
Enter value for sigmaY:
0.7788007830714049
0.5
0.5
1
0
0
1
1
3

Question 2 [25 points]
In this question, you will write some code using the Turtle module to create a drawing. There are no specific shapes you must draw, but your code must produce a drawing that satisfies at least the following basic requirements:
• A circle should be drawn inside another circle.
• Two shapes (not including the circles), each with a different number of sides, should be drawn
separate from each other, with no line between them.
• The first letter of your first name should be drawn in the bottom-right area. (You must sign your artwork!)
• At least one shape should be drawn using a for loop.
Any submission meeting these requirements will get full marks, but you are encouraged to go beyond
them. The most creative submissions (as judged by our TAs) will be shown in class.
Note: You can import the speed function from turtle and then call speed(“fastest”) to speed up drawing, so that you don’t waste time when testing your code.
Examples (as executed in Thonny)
Example 1:
>>> %Run artwork.py
4

Question 3 [25 points]
A local museum is arranging for the transport of glass spheres from London for an upcoming exhibition. To arrange for shipping and calculate costs, the museum received data on each sphere: its radius, volume and surface area. However, the file containing the data was corrupted after a ransomware attack. Now, the museum only knows one piece of data about each sphere: either the radius, the volume, or the surface area. You must write a program that asks the user which piece of data is present for a sphere (radius, volume, or surface area), and then, using that piece of data, calculate the two other missing pieces of data for that sphere. Finish by printing all three pieces of data (radius, volume and surface area) to the screen.
Recall that the volume of a sphere with radius r is given by 4πr3 and surface area by 4πr2. 3
If the user does not type in one of ‘radius’, ‘volume’, or ‘surface area’ when asked, or if the user enters a negative number when asked to type the value, your program should print ‘Invalid input.’ and stop (without printing anything else). You can assume that when asked to type the value, the user will always enter a number (no letters or symbols).
Hint: Try writing code that just handles the case where ‘radius’ is entered. Once you have perfected that, start on the case where ‘volume’ or ‘surface area’ is entered.
Filename
You must write this program in a file called sphere.py.
User input
• A string for the piece of data that was not corrupted (‘radius’, ‘volume’ or ‘surface area’).
• The value (number) for that piece of data. (You can assume the user will always enter a
number.)
Examples (as executed in Thonny)
Example 1:
Example 2:
>>> %Run sphere.py
What piece of data do you have? radius
Enter the number: 5
You entered 5.0 for the radius.
The volume is 523.5987755982989 and the surface area is 314.1592653589793.
>>> %Run sphere.py
What piece of data do you have? volume
Enter the number: 50
You entered 50.0 for the volume.
The radius is 2.285390748670416 and the surface area is 65.63429036687326.
5

Example 3:
Example 4:
Example 5:
>>> %Run sphere.py
What piece of data do you have? surface area
Enter the number: 100
You entered 100.0 for the surface area.
The radius is 2.8209479177387813 and the volume is 94.03159725795935.
>>> %Run sphere.py
What piece of data do you have? circumference Invalid input.
>>> %Run sphere.py
What piece of data do you have? radius Enter the number: -9001
Invalid input.
6

Question 4 [40 points]
Dr. Vink of the Math Lab has made a conjecture about a particular sequence of numbers that he
calls the Vink sequence. He claims that for any odd integer k > 0, a sequence of numbers can be
constructed as follows. Start with an integer n > 0. Then, the next term in the sequence is n if 2
n is even, or 3n + k if n is odd. Vink claims that this sequence will eventually produce a term of 1 after some number of terms. For example, starting with n = 9 and k = 1, we get the sequence 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, which reaches 1 after 20 terms.
To help check Vink’s claim, we will write code that checks how many terms it takes to converge to 1 with given values of n and k. You must write a function vink sequence(n, k, j) that computes the first j terms of the Vink sequence for the given values of n and k. If none of the j terms are 1, then the function should return -1. If you do encounter a term of 1, then the function should stop computing terms and immediately return the number of terms that have been computed so far. For example, if the 37th term of the sequence is 1, then the function should immediately return the value 37.
Next, outside your function, ask the user to type in a value for n, k and j. If either n <= 0, k < 0, or j <= 0, print ”Invalid input.” and end the program. Otherwise, call vink sequence(n, k, j) with the user’s inputs, and print the result to the screen. Filename You must write this program in a file called vink.py. User input • Three integers: n > 0, k >= 0 and j > 0. (You can assume the user will always enter numbers, without any letters/symbols, etc.)
Examples (as executed in Thonny)
Example 1:
Example 2:
>>> %Run vink.py
Enter n:
Enter k:
Enter j:
112
27
1
1000
>>> %Run vink.py
Enter n:
Enter k:
Enter j:
-1
9
2
5
7

Example 3:
Example 4:
>>> %Run vink.py
Enter n:
Enter k:
Enter j:
Invalid input.
5
-2
100
>>> %Run vink.py
Enter n:
Enter k:
Enter j:
Invalid input.
0
5
10
8