Python代写 | COMPS258 Computer Programming and Problem Solving

COMPS258 Assignment 3 (Autumn 2020)

Question 1 Programming (52%) Specific Requirements of Question


Data Validation

The range of the input from the user and parameter values should be checked to avoid causing your program to crash. This is about checking the range and see if the value making sense and not causing an execution error.

For example, the height of square should be check against negative value (which does not make sense) (which would cause the program to crash).

However, you need not deal with the cases that have been assumed in the question itself.

A telephone number is considered lucky if the number satisfies all the following conditions.

Complete the following function that accepts a telephone number (a string) as a parameter and returns True if it is a lucky number and False if otherwise. Assume the input parameter is always correct.

Submit [8 marks]

A comma separated list of values is a string containing items separated by commas. The following is an example of a comma separated list of numbers.

Complete the following function that accepts a comma separated list of numbers (a string) as a parameter, and then returns the average of the numbers. The function should ignore any item in the comma separated list that is not in a number format.

The function should raise a ValueError with a suitable message if the parameter is not a string. Assume that the comma separated list has at least one number.

Submit [8 marks]
Hint: use the function split(‘,’) to divide a comma separated list into a Python list of items.

11:47 AM 2/23/21 3

COMPS258 Assignment 3 (Autumn 2020)

A file assn_scores.txt is given to you that contains the assignment scores of students in a class. Each line in the file contains a comma separated list of 4 items: student name, scores for assignment 1, 2, and 3 respectively.
The overall score is the sum of 40% of assignment 1, and 30% from assignment 2 and 3. Write a complete program that analyzes the file and finds out the student with the highest overall score. If there are two or more students with the highest score, choose anyone.

The program should print the findings to a file named highest.txt. The output format should look like the following example. There is no particular requirement on the format of numbers.

The program should require no input from the users, and it should print ‘Analysis finished’ to the screen before the end of execution.

The program should also print ‘IO Error’ if there is an error in file operations. Submit [12 marks]

The spaghetti numbers are defined as the following function spa with integer parameter N:

This is a recursive relation, and it can be implemented using a recursive function. Complete the following recursive function that returns the spaghetti number of N. Assume that the parameter N is a positive integer.

Submit [8 marks]

def spa(N):

This question is about the BigO notation.

(i) Estimate the number of steps needed for procedures with the following Big-O. Assume that one step is needed for each input data and the input data size (N) is 8192.

Big-O of Procedure Number of Steps O(N)
O(lg N)
O(N lg N)

(ii) There are two algorithms: algorithm A and algorithm B are found to have O(N2) in their worst-case performance. It is also found that algorithm A is usually slower than algorithm B by 0.05 seconds. Comment whether it is possible.

Submit your answers in q1e.txt. [8 marks]

Apply selection sort to the following words by hand according to the following requirements.

11:47 AM 2/23/21 4

COMPS258 Assignment 3 (Autumn 2020)

 When two words are compared, the “larger” word is the longer one, and if the length is equal, the alphabetically order is used. The case is ignored in the comparison (e.g. ‘A’ and ‘a’ are considered the same).

Submit your work in q1f.txt. Show your working step by step on each line in the text file. On the last line, report the total number of comparisons required. [8 marks]

Question 2 Middle Level Programming (36%) Specific Requirements of Question


Error Tolerance Efficiency

The functions you write should handle erroneous input and parameters and report an error if necessary.

You should avoid overly inefficient coding in your programs.

The skeleton program file contains the Stock Searching program ( described in Unit 6 (for Activity 6.4). The current function searchByCode uses sequential search. Anders suggests improving the program by changing to binary search.

‘Stock Code’

Submit and q2a.txt. [16 marks]

Do you notice that the recursive way of finding spaghetti numbers is slow? Let’s check how slow it is.

Copy your recursive implementation for the spaghetti numbers to the program file Add main program code in the file to find out the time taken (in seconds) to get the spaghetti numbers of N = 4, 5, 6, 7 and 8.

Submit and q2b.txt. [12 marks]

This is not a real question. You will earn these 8 marks only if your submission is made correctly. Please refer to the document Instruction on Electronic Submission for the details. [8 marks]