# Python代写 | COMPS258 Computer Programming and Problem Solving

COMPS258 Assignment 3 (Autumn 2020)

Question 1 Programming (52%) Specific Requirements of Question

Specifics

(a)

 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.

•   8 digits
•   Among the first, third, fifth, and seventh digits, at least two of them are the same (e.g., ‘11366311’, ‘ 42277224’).
•   The first 4 digits is the reverse of the last 4 digits (e.g., ‘ 81166118’).

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 q1a.py. [8 marks]

```def isLuckyPhoneNum(phone):
```

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.

```"15.4,22.3,10,-2,45, 82 , 111.5, -12, 35"
```

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 q1b.py. [8 marks]
Hint: use the function split(‘,’) to divide a comma separated list into a Python list of items.

```def process_cslist(cslist):
```

(b)

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.

``` Highest Overall Score is [Highest Score] by [Student Name]
The Scores are [ASSN 1 Score], [ASSN 2 Score] and [ASSN 3 Score]
```

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 q1c.py. [12 marks]

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

```    spa(0) = 0
spa(1) = 1
spa(2) = 1
spa(N) = spa(N-3) * 2 + spa(N-2) + spa(N-1)
```

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 q1d.py. [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)

O(N2)

(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.

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

•   Ascending order (Largest at the right-most).
•   Use left-to-right progress order (the sorted data starts from the left to right).

(c)

(d)

(e)

(f)

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).

```    Toyota Saab Kia Dongfeng Lexus Jacquard BMW Skoda
```

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

Specifics

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 q2a.py contains the Stock Searching program (searchStock.py) 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.

1. (i)  Binary search requires the list of stock codes (list of OrderedDict) to be sorted according to the ‘Stock Code’ field. The current version assumes that the data file has the data sorted, which is not reliable. Make changes to the function readStockData in q2a.py.
•   Convert the ‘Stock Code’ field (currently string type) to an integer.
•   After file reading, sort the list of stock codes in ascending order based on

‘Stock Code’

2. (ii)  Complete the function binarySearchByCode in q2a.py that has the same functionality as searchByCode, but the implementation should use binary search.
3. (iii)  Anders wishes to test if binarySearchByCode is faster than searchByCode but he is not sure how to do that. Explain whether he should test for the best case or the worst case, and how to do that in the file q2a.txt.

Submit q2a.py 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 q2b.py. 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.

1. (i)  Submit your findings (i.e., the time taken) in q2b.txt, and the main program testing code in q2b.py.
2. (ii)  Explain the relation, if any, between the time taken and N in q2b.txt.
3. (iii)  Complete the function spa_iter that implements a non-recursive way to find the spaghetti numbers. This function should use a for or while structure. There is no need to include testing for this function in the main program code.

Submit q2b.py and q2b.txt. [12 marks]

```def spa_iter(N):
```

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]

(a)

(b)

(c) E-mail: vipdue@outlook.com  微信号:vipnxx 