Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

ENGG1811 Computing for Engineers

Sample Exam, Term 3, 2022

Instructions

Time allowed: 2 hours and 15 minutes

(b) This exam consists of 5 questions. The mark for each question is indicated.

(c) This exam is worth 40% of the overall assessment of this course.

(d) Each question requires you to submit a separate Python program le 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 link.

(iii) Each question requires a specific lename and the submission system will only accept that particular filename.

(iv) Ensure that you save your le before submission.

(v) You can make multiple submissions during the exam. Only the last submitted file will be assessed.

(e) You are allowed to consult course materials on the course website, as well as the Python and

numpy documentations provided at the Lab 10 page.

(f) UNSW exam rules apply. No communications are allowed.

Question 1 (20 marks)

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

def  q1_func (p,d,x):

where the inputs p, d and x are numbers of int or float type. The function q1_func is expected to return an output of str type.

The function q1_func is required to perform these tasks:

Task 1 It should rst check whether the inputs d and x are both valid. The input d is valid if it is greater than zero. The input x is valid if it is greater than 0 and less than 2. If both d and x are valid, the function should proceed to perform Task 2; otherwise, the function should return the string

'invalid  parameters' and exit the function. Note that there are no restrictions on p.

Task 2 The function should calculate z using the formula:

p     

z   =

The function q1_func should return a string which depends on the value of z, according to the following:

  If z is greater than or equal to 50.2, the string to be returned is 'excellent'

  if z is less than 50.2, and greater than or equal to 37. 1, the string to be returned is 'good'

  If z is less than 37. 1, the string to be returned is 'fair'

You can assume that when we test your program, the inputs p, d and x are always of the type float or int.

Note that the test file test_q1 .py checks only for one possible scenario of invalid parameters.

Requirements and testing:

• You must write the function q1_func in a le with the lename q1 .py.  The submission system will only accept this filename. A template file q1 .py has been provided.

You can use the le test_q1 .py for testing but note its limitation stated above.

 You do not need to submit test_q1.py.

Make sure that you save your file before submission.

Question 2 (20 marks)

You are not allowed to use numpy for this question.  The use of numpy for this question will

result in a score of 0 marks.

Your task is to write a Python function q2_func with the following def line: def  q2_func (a_list,intervals):

where

The input a_list is a list of numbers of int type or an empty list.

• The input intervals is a list of lists. An example of intervals is  [[0,4],[8,12]]. You can assume that, when we test your program, the input intervals always contains 2

lists, and each list in intervals has exactly 2 numbers of int type.

The function is required to return a list.

We will use the following example to illustrate what your you need to do.  Assuming that the inputs are:

a_list  =   [ 10 ,5 ,3 ,7 ,5 ,7 , 6 , 9 ,3 ]

intervals  =   [[ 0 ,4 ],[ 8 , 12 ]]

The lists in intervals are used to specify two closed intervals on the number line. The list [0,4] is used to specify the closed interval [0, 4] which contains all numbers that are greater than or equal to 0, and less than or equal to 4.  Similarly, the list  [8,12] is used to specify the closed interval [8, 12] which contains all numbers that are greater than or equal to 8, and less than or equal to 12. The function q2_func should return all the numbers in the input a_list which lies in at least one of the two intervals specified by the input intervals. For this example, the expected output of the function is the list [10,3,9,3] because both the numbers 10 and 9 are in the interval [8, 12] and the number 3 is in the interval [0, 4]. Note that we require that the numbers in the output list are arranged in the same order that they appear in the input a_list.

If none of the numbers in the input a_list is in either interval, or if the input a_list is an empty list, then the function should return an empty list as the output.

You can assume that, when we test your program, we will only use a_list and intervals of the format stated above. In addition, you can assume that for each list in intervals, the second entry in the list is strictly greater than the first entry.

Requirements and testing:

• You must write the function q2_func in a le with the lename q2 .py.  The submission system will only accept this filename. A template file q2 .py has been provided.

You can use the le test_q2 .py for some basic testing.

 You do not need to submit test_q2.py.

Make sure that you save your file before submission.

Question 3 (20 marks)

For this question, your task is to write a Python function q3_func with the following def line: def  q3_func (expt_data,outdoor_temp_data,threshold):

where

• The input expt_data is a NumPy 2-D array whose entries are of float dtype.  You can expect this array to be non-empty.

• The input outdoor_temp is a NumPy 1-D array whose entries are of float dtype.  The number of entries in outdoor_temp is expected to be the same as the number of columns in expt_data.

 The input threshold is a float.

The function is required to return an int as the output.

We will use the following example to illustrate what you need to do for this question.  For this example, the inputs are:

expt_data  =  np .array([[ 9.6 ,  3.5 ,  1.7 ,  2.8 ],

[2.2 ,  1.1 ,  1.3 ,  1.6 ],

[2.4 ,  1.0 ,  1.1 ,  1.3 ],

[ 1.7 ,  0.9 ,  4.9 ,  0.7 ],

[ 0.6 ,  2.0 ,  0.3 ,  0.5 ]])                      outdoor_temp_data  =  np .array([34.3 ,  21.6 ,  31.7 ,  21.1 ]) threshold  =  31.2

You can interpret the array expt_data as a collection of experimental data where each row corre- sponds to the measurements from a sensor. We will refer to each sensor using its row index. For this example, the measurements from Sensor 0 are stored in the row with index 0, and these measurements are 9.6, 3.5, 1.7 and 2.8.

Each column corresponds to the measurements from a day.  For this example, the column with index 3 corresponds to the measurements taken in Day 3, and these measurements are 2.8, 1.6, 1.3, 0.7 and 0.5. The array outdoor_temp contains a separate set of measurements which can be inter- preted as the outdoor temperature. These outdoor temperature measurements were taken on the same days as those in expt_data, e.g., in Day 3, the outdoor temperature measurement is the entry in outdoor_temp with index 3, which has the value of 21. 1.

For each sensor, you will need to compute the average of those temperature measurements from the days whose outdoor temperature measurements are at or above the value in threshold. For this example, threshold is 31.2 and the outdoor temperature measurements in Days 0 and 2 are at or above this value. Therefore, for Sensor 0, you will compute the average of 9.6 and 1.7, which are the

measurements from Sensor 0 in Days 0 and 2; similarly, for Sensor 4, you will compute the average of 0.6 and 0.3.

Question to be continued on the next page.

Your program is to return an int which is the index of the sensor which has the highest average temperature over those days whose outdoor temperature is at or above the threshold. For this exam- ple, these averages from Sensors 0 to 4 are 5.65 (which comes from the average of 9.6 and 1.7), 1.75, 1.75, 3.3 and 0.45, so the program should return 0 because the computed average from Sensor 0 is the highest.

You can assume that when we test your program, the inputs expt_data and outdoor_temp are of the format stated above. In addition, you can assume that at least one value in outdoor_temp is at or above threshold, and there is a unique sensor that has the maximum.

Requirements and testing:

• You must write the function q3_func in a le with the lename q3 .py.  The submission system will only accept this filename. A template file q3 .py has been provided.

 You can use the le test_q3 .py for testing.

 You do not need to submit test_q3.py.

Make sure that you save your file before submission.

Question 4 (20 marks)

Note that for this question: (1) You are not allowed to use NumPy. (2) There is a differential marking scheme which will be explained later.

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

def  q4_func(a_list):

where a_list is a Python list whose entries are of data type int. The function is expected to return an int.

The aim of q4_func is to locate the rst occurrence of a specific pattern in the input a_list. The pattern to be located is that three consecutive entries in a_list are equal. The function is to return the positive index of the middle entry in the first occurrence of such pattern if the pattern can be found in a_list. If the pattern is not present in the input, the function should return − 1.

The examples in the following table illustrates the requirements on q4_func further. Note that these examples are the test cases in the test file test_q4 .py.

Test Case

a_list

Expected output

0

[6,  1,  7,  7,  7,  9]

3

1

[5,  3,  3,  3,  3,  3,  5]

2

2

[2,  3,  2,  9,  3,  5,  10,  9,  9,  9,  8,  3,  3,  3]

8

3

[2,  3,  2,  9,  3,  5,  10,  9,  6,  9,  8]

 1

For Test Case 0, three consecutive equal entries 7,7,7 occur in a_list. The middle entry has

index 3 and this index should be returned.

For Test Case 1, the first occurrence of three consecutive 3’s occurs at indices 1, 2 and 3. The

function should return 2.

For Test Case 2, the first occurrence of the pattern is the three 9’s at indices 7, 8 an 9. The function

should return 8.

For Test Case 3, the pattern is not found in the list. The function should return − 1.

Differential marking: We want you to use an efficient algorithm based on either while, while/break or for/break to solve this problem.  To be efficient, your program should break the loop as soon as the first occurrence of the pattern has been found. If your algorithm is efficient, you may get the maximum mark; otherwise, the most you can get is 70% of the maximum.

Question to be continued on the next page

Requirements and testing:

• You must write the function q4_func in a le with the lename q4 .py.  The submission system will only accept this filename. A template file q4 .py has been provided.

• You can use the le test_q4 .py for testing. Note that the test le test_q4 .py is written so that you can only test with only one test case at a time. You can select the test case using the variable test_index in Line 14. This is in case you run into a problem with an infinite loop if you use while.

 You do not need to submit test_q4.py.

Make sure that you save your file before submission.

Question 5 (20 marks)

A friend of yours has made a mistake in their computer program.  Instead of producing a list of float’s such as:

[3.5 ,  121.4 ,  72.3 ]

their program has erroneously produced the following string

'3 .5121 .472 .3'

You can see that, if you turn the floats in the list into strings and concatenate those strings, then you will obtain the erroneous string. Luckily, your friend remembers that all the floats in each list that they wanted to generate have the same number of decimal places and they remember what the number of decimal places is. This means it is possible for you to write a program to help your friend to recover the list from the erroneous string.

Your task for this question is to write a Python function q5_func with the following def line: def  q5_func(string,  num_decimals):

where string is an erroneous string in the format stated above and num_decimals is the number of decimal places in the floats in the list. The function is required to return a list of floats which is the correct list that your friend wanted to produce. The following table gives a few examples to illustrate the expected output given the inputs.

string

num_decimals

Expected output

'3 .5121 .472 .3'

1

[3 .5,  121 .4,  72 .3]

'154 .7987 .70171 .72139 .4718 . 84'

2

[154 .79,  87 .70,  171 .72,  139 .47,  18 . 84]

'773 . 956438 . 878858 .598697 .368'

3

[773 . 956,  438 . 878,  858 .598,  697 .368]

You can assume the following: (1) The original list that produces the erroneous string has at least one float and the number of decimal places is at least 1.  In addition, the original list contains only positive numbers. (2) The input string has the right format and is non-empty. (3) When the erroneous string is formed, zeros are padded at the end to make sure that it has the right number of decimal places. For example, if the number is 87.7 and the number of decimal places is 2, this number will appear at 87.70 in the string, see the example in the middle row of the table.  (4) The input num_decimals is an int which is greater than zero.

Hint: One possible method is to use split. Note that a string is a sequence of characters, see page 3 of Lecture Week 9A. You can slice a string in the same way that you slice a list, e.g. if x  =   'billy' then x[1:4] is the string 'ill'.

Question to be continued on the next page

Requirements and testing:

• You must write the function q5_func in a le with the lename q5 .py.  The submission system will only accept this filename. A template file q5 .py has been provided.

 You can use the le test_q5 .py for testing.

 You do not need to submit test_q5.py.

Make sure that you save your file before submission.