0% found this document useful (0 votes)
7 views

EMS412 Python Practice Test Solutions

The document contains a series of programming tasks that require the completion of functions for various mathematical operations, including multiplication of three numbers, calculation of the area of a circle, area of a segment of a circle, calculation of Euler's number, position of an object under constant acceleration, and implementation of a step function. Each task includes autograded tests to validate the correctness of the implemented functions. The document provides hints and structure for completing the functions effectively.

Uploaded by

irfan141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

EMS412 Python Practice Test Solutions

The document contains a series of programming tasks that require the completion of functions for various mathematical operations, including multiplication of three numbers, calculation of the area of a circle, area of a segment of a circle, calculation of Euler's number, position of an object under constant acceleration, and implementation of a step function. Each task includes autograded tests to validate the correctness of the implemented functions. The document provides hints and structure for completing the functions effectively.

Uploaded by

irfan141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

 ID: question Read-only

Q1: complete the function that will return the multipication of 3 given numbers - a,b,c.

In [17]: ID: mult_3_numbers Autograded answer

1 def multiply_numbers(a,b,c):
2 """insert the code that performs the operation.
3 Use the variable 'number' to hold and return the value """
4 ​
5 ### BEGIN SOLUTION
6 ​
7 number = a*b*c
8 ### END SOLUTION
9 return number
10 ​

In [18]:  Points: 2 ID: solution_mult_3_numbers Autograder tests

1 # Don't write code in this line


2 ​
3 ### BEGIN HIDDEN TESTS
4 ​
5 assert multiply_numbers(3,4,5) == 60
6 ​
7 assert multiply_numbers(2,2,2) == 8
8 ​
9 assert multiply_numbers(-2,1,1) == -2
10 ### END HIDDEN TESTS
11 ​

 ID: cell-c55d1fb40a087633 Read-only

Q2: complete the function that returns the area of a circle with radius r , note, use math.pi for pi.
In [19]: ID: circle_area Autograded answer

1 import math
2 def circle_area(r):
3 """calculate the area given radius r,
4 use the variable named 'area' to hold and return the correct value """
5 ### BEGIN SOLUTION
6 area = math.pi*r**2
7 ### END SOLUTION
8 return area
9 ​

In [20]:  Points: 2 ID: cell-d09cd3c4dd5b0642 Autograder tests

1 # Don't write code in this line


2 ​
3 ### BEGIN HIDDEN TESTS
4 import math
5 assert circle_area(2) == math.pi*2**2
6 ​
7 assert circle_area(1) == math.pi*1**2
8 ### END HIDDEN TESTS

 ID: cell-1fad1c57ead146df Read-only

sector of a circle with radius r and angle theta (in degrees). Note, use math.pi for
Q3: complete the function that returns the area of a segment
pi.
In [21]: sector
ID: area_segment Autograded answer

1 import math
2 sector
def segment_area(r,theta):
3 sector’s area.
"""calculate the area given radius r and angle theta, return the segment's
4 Use the variable named 'area' to hold and return the correct value """
5 ### BEGIN SOLUTION
6 area = (theta/360.)*math.pi*r**2
7 ### END SOLUTION
8 return area
9 ​ sector’s
10 ​

In [22]:  Points: 3 ID: cell-cf7afb549d01e873 Autograder tests

1 # Don't write code in this line Text


2 ​
3 ### BEGIN HIDDEN TESTS
4 sector
assert abs(segment_area(2,90) - 3.141592653589793) < 1.e-6
5 ​
6 sector
assert abs(segment_area(1,180) - 1.5707963267948966) < 1.e-6
7 ​
8 sector
assert abs(segment_area(4,270) - 37.69911184307752) < 1.e-6
9 ​
10 ​
11 ### END HIDDEN TESTS

 ID: cell-1e7805d0c8f9cfca Read-only

𝑒
Q4: complete the function that calculates the Euler's number from the series you learnt during the maths lessons. The funcion takes in
the value n, this will be the term that you truncate the series at. Also use the following hints.

Hint 1: remember the Maclaurin Expansion 𝑒𝑥 = 1 + 𝑥 + 𝑥2!2 + 𝑥3!3 +...... 𝑥𝑖!𝑖 +....
Hint 2: you will need to use a for loop,

Hint 3: 𝑒 = 𝑒1 , when x=1 for 𝑒𝑥


Hint 4: use the math.factorial(i) to caluclate factorial of i THAT IS: i! = math.factorial(i)
In [9]: ID: calculate_e Autograded answer

1 import math
2 def calculate_e(n):
3 """ Here use the variable e to denote and return the value of e.
4 I have initiated the value of e (e=0) and defined the point where you will evaluate the series - that is
5 The value of variable n denotes the number of terms to use in the series.
6 So write the loop to compute the first n terms. """
7
8 e = 0.
9 x = 1
10
11 ### BEGIN SOLUTION
12 ​
13 for i in range(n+1):
14 e = e + x**i/math.factorial(i)
15
16 ### END SOLUTION
17 return e
18 ​
19 print(calculate_e(8))

2.71827876984127

In [24]:  Points: 3 ID: cell-2fcd40d0098ccdb0 Autograder tests

1 # Don't write code in this line


2 ​
3 ### BEGIN HIDDEN TESTS
4 ​
5 assert abs(calculate_e(2) - 2.0 ) < 1.e-6
6 ​
7 assert abs(calculate_e(4) - 2.666666666666 ) < 1.e-6
8 ​
9 assert abs( calculate_e(8) - 2.7182539682539684 ) < 1.e-6
10 ### END HIDDEN TESTS

 ID: Q5_question Read-only

Q5: Complete the function that calculates the position of an object at any time t≥ 0. The initial position and the initial velocity (at time t=0)
are denoted as s0 and v0 respectively. The object undergoes constant acceleration a, for any time t>0.

In [25]: ID: cell-8e3ba0502d731ebc Autograded answer

1 def calculate_position(s0,u0,a,t):
2 """given the initial conditions and acceleration, return the object's position.
3 Use the variable named 'position' to hold and return this value """
4 ### BEGIN SOLUTION
5
6 position = s0 + t*u0 + 0.5*a*t**2
7 ### END SOLUTION
8 return position
9 ​
10
11 ​
12 ​

In [26]:  Points: 2 ID: cell-20f2285ef5b06d58 Autograder tests

1 # Don't write code in this line


2 ​
3 ### BEGIN HIDDEN TESTS
4 assert abs( calculate_position(1,1,1,1) - 2.5 ) < 1.e-6
5 assert abs( calculate_position(2,2,2,2) - 10.0 ) < 1.e-6
6 assert abs( calculate_position(1,0,0,0) - 1.0 ) < 1.e-6
7 assert abs( calculate_position(0,2,0,1) - 2.0 ) < 1.e-6
8 ​
9 ### END HIDDEN TESTS

 ID: cell-32db3317caeae586 Read-only

𝑥
Q6: Complete the function that takes in a number , and returns the value 1 if 𝑥 ≥ 0, otherwise it is 0 - this is called a step function
In [27]: ID: cell-707062d257fe4a7f Autograded answer

1 def step_function(x):
2 """given x compute the correct value to return.
3 Note you need to include the return statement here"""
4
5 ### BEGIN SOLUTION
6 value = 0
7 if x >= 0 :
8 value = 1
9 return value
10 ### END SOLUTION
11 ​

In [28]:  Points: 2 ID: cell-49b8190403377f77 Autograder tests

1 # Don't write code in this line


2 ​
3 ### BEGIN HIDDEN TESTS
4 ​
5 assert step_function(-1) == 0
6 ​
7 assert step_function(0) == 1
8 ​
9 assert step_function(1) == 1
10 ​
11 ​
12 ### END HIDDEN TESTS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy