### ENGG1811 Week 10 Lab

ENGG1811 Week 10 Lab

Instructions

(a) This lab consists of 4 questions.

(b) Time allowed: 5 minutes of reading time + 55 minutes of working time.

(c) Each question requires you to submit a separate Python program file for marking. Note the

following:

(i) For each question, an associated test file has been provided to help you to test your code.

It is your responsibility to test your code thoroughly before submission.

(ii) Submission must be made using the submission box.

(iii) There is a tab for each question in the submission box.

(iv) Each question requires a specific filename and the submission system will only accept that

particular filename.

(v) Ensure that you save your file before submission. If the submission system accepts your

file, it will run tests on your submitted file.

(vi) You can make multiple submissions during the lab. Only the last submitted file will be

assessed.

(d) You are allowed to consult the following Python documentation in PDF format. These documents

are available from the background menu, which can be accessed by right-clicking on the

blue background.

The Python Tutorial (Release 3.7.5)

Reference on the Python math library functions

The NumPy User Guide (Release 1.16.1)

The NumPy Reference (Release 1.16.1)

(e) If you have accidentally killed Spyder3, the submission box, PDF file reader or file manager,

you can re-start them from the background menu. The calculator app is also available on the

background menu.

1

Question 1

Bush walkers use a simple equation, called Naismith’s Rule, to estimate the amount of time taken in

hours for a walk given its distance d in kilometres, climb h (number of metres of height gained over

all uphill sections) and the average walking speed s in km/hr on flat parts of the walk:

t =

d

s

+

h

400

For example, a 21km walk with a 600m climb undertaken at an average speed of 3.5 km/hr should

take about 21/3.5 + 600/400 = 6.0 + 1.5 = 7.5 hours.

A walk is classified as Easy if it takes less than 4 hours and also the climb is less than 200m; it

is Hard either if it takes more than 8 hours or the climb is more than 600m; and it is Medium in any

other case.

Write a Python function that returns the classification of a walk (one of the strings ”Easy”,

”Medium” or ”Hard”), given parameters d, h and s. The def line of the function should be:

def q1_func(d,h,s):

Requirements and testing:

You must write the function q1_func in a file with the filename q1.py. The submission

system will only accept this filename. A template file q1.py has been provided.

You can assume that we will only use positive values of d, h and s for testing.

You can use the file test_q1.py for testing.

You do not need to submit test_q1.py.

Make sure that you save your file before submission.

2

Question 2

Note that if this question appears in the exam, a differential marking scheme may be used. For a

differential marking scheme, you can only receive the maximum mark if your solution is correct

and it does not use any loops, while a correct solution that uses loops will receive only a fraction

(possibly 70%) of the maximum. However, for this lab, a differential marking scheme will not

be used but you are encouraged to try to solve this problem without using loops.

Your task is to write a Python function q2_func with the following def line:

def q2_func(x,y):

The function inputs x and y are assumed to be 1-dimensional numpy arrays of the same shape.

The function is required to compute and return a numpy array which has the same shape as both x and

y. In the following description, we will refer to the numpy array to be returned by the variable name z.

Let x[i], y[i] and z[i] be the element indexed by i in, respectively, the arrays x, y and z.

The relationship between x[i], y[i] and z[i] is given by the following pseduo-code:

if (absolute value of x[i] > absolute value of y[i])

z[i] = x[i] – y[i]/2

else

z[i] = y[i] – x[i]/2

For examples:

If x[i] is 4 and y[i] is 3, then z[i] should take on the value of 4 – 3/2 = 2.5.

If x[i] is 3 and y[i] is -4, then z[i] should take on the value of -4 – 3/2 = 5.5.

Requirements and testing:

You must write the function q2_func in a file with the filename q2.py. The submission

system will only accept this filename. A template file q2.py has been provided.

Your function must be able to work with any two 1-dimensional numpy arrays x and y of the

same shape. You can assume that we will only test your function with a pair of arrays of the

same shape.

You can use the file test_q2.py for testing.

You do not need to submit test_q2.py.

Make sure that you save your file before submission.

3

Question 3

Note that if this question appears in the exam, a differential marking scheme may be used. For a

differential marking scheme, you can only receive the maximum mark if your solution is correct

and it does not use any loops, while a correct solution that uses loops will receive only a fraction

(possibly 70%) of the maximum. However, for this lab, a differential marking scheme will not

be used but you are encouraged to try to solve this problem without using loops.

Your task is to write a Python function q3_func with the following def line:

def q3_func(x,lower,upper):

where

The input x is a 2-dimensional numpy array

The inputs upper and lower are two scalars of the datatype float. You can expect that the

value of upper is strictly larger than that of lower.

The function is required to return a scalar value, which is the number of times that the variance

of the columns of the array x is in the interval between lower and upper exclusively, i.e. does not

include the ends.

For the array x_1 in the test file q3_test.py, the variances of the 10 columns of the array are:

[0.114, 0.050, 0.022, 0.042, 0.054, 0.298, 0.066, 0.068, 0.330, 0.266]

If upper and lower take on the value of 0.3 and 0.1, then there are 3 columns in the interval

between 0.1 and 0.3. The expected answer is 3 and the function is expected to return this value for

these inputs.

Requirements and testing:

You must write the function q3_func in a file with the filename q3.py. The submission

system will only accept this filename. A template file q3.py has been provided.

Your function must be able to work with any 2-dimensional numpy array, and any value of

lower and upper. You can assume that for all the tests, the value of lower is smaller than

that of upper.

The numpy function for computing variance is called var().

You can use the file test_q3.py for testing.

You do not need to submit test_q3.py.

Make sure that you save your file before submission.

4

Question 4

In order to describe the processing that you need to code for this question, we will refer to the file

q4_illustration.py. There are 4 illustrative examples in the file. The first example has been

reproduced below:

# Illustration 1

# Given the array x_1

x_1 = np.array([-2, 6, 0, -23, 1.9, -4.5, 7.3, -5.1])

# and the number of entries to be averaged at a time = 4

# Want to compute y_1

y_1 = np.zeros((5,),dtype = float)

y_1[0] = np.mean(x_1[0:4])

y_1[1] = np.mean(x_1[1:5])

y_1[2] = np.mean(x_1[2:6])

y_1[3] = np.mean(x_1[3:7])

y_1[4] = np.mean(x_1[4:8])

You are given a 1-dimensional numpy array x_1 and a positive integer which is 4 in this example.

The positive integer tells you the number of entries that you want to average at a time. The first step

is to compute the mean of first 4 elements of x_1, i.e. the mean of x_1[0], x_1[1], x_1[2]

and x_1[3]. After that, you compute the mean of x_1[1], x_1[2], x_1[3] and x_1[4]. You

keep on doing this until you have computed the last 4 elements in x_1, i.e. the mean of x_1[4],

x_1[5], x_1[6] and x_1[7]. You also want to store these means in a numpy array as illustrated

in the example above.

The second example is similar to the first except the given integer has a value of 6. The third

example describes the processing needed when the given positive integer is equal to the number of

entries in the given array. The fourth example describes the processing for the case where the given

positive integer is 1.

Your task is to write a Python function q4_func with the following def line:

def q4_func(array,n):

where

The input array is a 1-dimensional numpy array

The input n is a positive integer. You can assume that the value of n is no more than the number

of entries array.

The function is required to return a numpy array containing the mean of n elements of array at

a time, according to the examples in q4_illustration.py.

Requirements and testing:

You must write the function q4_func in a file with the filename q4.py. The submission

system will only accept this filename. A template file q4.py has been provided.

5

Your function must be able to work with any 1-dimensional numpy array, and any value of

positive integer n. You can assume that for all the tests, the value of n is no more than the

number of elements in the array and the array is not empty.

You can use the file test_q4.py for testing.

You do not need to submit test_q4.py.

Make sure that you save your file before submission.

6