CS代考 COMPS258 Assignment 2 (Autumn 2021)

COMPS258 Assignment 2 (Autumn 2021)
Question 1 Programming (60%) Specific Requirements of Question
Parameter Validation Input Validation
The range of the parameter values of functions should be checked to avoid causing the function to crash. This is about checking the range and see if the value making sense and not causing an execution error.

Copyright By PowCoder代写 加微信 powcoder

Assume that the input is correct, unless specified in the question.
Write a Python program that prints a square of size depending on an entered value. The program first reads in the size of the square, and then prints the square with a pattern according to the examples below. The figure shows two different executions of the program. Submit your solution in q1a.py. [8 marks]
Enter size of pattern: 5 $$$$$
$$+++ $++++
Enter size of pattern: 8 $$$$$$$$
Write a Python program that prints a square of size depending on an entered value. The program first reads in the size of the square, and then prints the square with a pattern according to the examples below. The figure shows two different executions of the program. Submit your solution in q1b.py. [8 marks]
Enter size of pattern: 5 $$$$$
$$567 $5678
Enter size of pattern: 8 $$$$$$$$
2:47 PM 1/27/22

COMPS258 Assignment 2 (Autumn 2021)
A very strange resturant in the unversity is selling hamburgers at different prices according to the GPA of the students. The price list of Bias Burger is given below.
3.5 or above
>= 3.0 and < 3.5 >= 2.5 and < 3.0 Below 2.5 Write a program that reads in the age and the GPA, and then prints the price. In addition, the program should validate the input. The following table shows the error message that should be printed for each type of input error. The user should be asked to re-enter in case of error. Input Error The input is empty The input is not a number The GPA is negative The GPA is (otherwise) not between 2.0 and 4.0 (inclusive) Error Message "The input is empty" "The input is not a number" "The GPA should be positive" "The GPA should be >=2.0 and <=4.0" The following shows an example execution of the program. Submit your solution in q1c.py. [8 marks] Enter GPA: B+ The input is not a number Enter GPA: The input is empty Enter GPA: 2.56 The price is $40 The Ngak Yan casino has some dubious practices. In the games using three 6-face dices, one of them is purposely replaced with faces of 1, 2, 3, 4, 5, 5 so that 6 will never come out. Assume that you know a normal 6-face die has faces 1, 2, 3, 4, 5, 6. For curiosity, you wish to know how the statistics of the outcomes, which is the sum of throwing the 3 dices (with one of them a cheat die 古惑的骰), will be like. Write a program that roll three dices of which one of the dices has the above defect, calculate the outcomes, collect the statistics and find out the probability of rolling each possible outcome. Roll 1000 times and then print out the frequency (number of times having each of the outcome value) and also print the corresponding graph. Submit your solution in q1d.py. [8 marks] The program should require no input. The output should look like the example below. Note that due to randomness the exact frequencies wil change. The bar chart should be scaled appropriately for analysis. 2:47 PM 1/27/22 4 COMPS258 Assignment 2 (Autumn 2021) 􏰀 Use the following to print the sum and frequency in right aligned style. print('{:>2} {:>5} ‘.format(s, freq), end=”)
Sum Freq 32
4 5 6 7 8 9
58 ***** 72 *******
104 **********
137 *************
132 *************
128 ************
107 **********
81 ******** 76 ******* 32 *** 23**
Cypto-currencies are getting crazier by the day. Your friend Anders hopes to make sense out of the daily rate of USD to a Bitcoin sampled once at 12pm everyday. Help him write a program for some simple analysis according to the followig tasks.
􏰀 Read in the (sampled) daily rate in a 5-day period and store them in a list o Include input data validation to check if the input is a positive
number. In case of error, print a suitable message and ask the user to
􏰀 Add code to print the maximum, minimum and the average daily-rate
􏰀 Add code to print an advice of “BUY”, “HOLD” and “SELL” according to the
following rules conceived by Anders based on the daily rate on the last day (i.e., day 5).
The day 5 rate is more than 20% below the average The day 5 rate is more than 20% above the average Otherwise
BUY SELL HOLD
Submit q1e.py. [12 marks]
The following shows an example execution of the program without input error.
Enter Bitcoin rate of day 1
Enter Bitcoin rate of day 2
Enter Bitcoin rate of day 3
Enter Bitcoin rate of day 4
Enter Bitcoin rate of day 5
The highest rate is 46000.0
The lowest rate is 38000.0
The average rate is 42800.0
(USD): 43000 (USD): 45000 (USD): 46000 (USD): 42000 (USD): 38000
2:47 PM 1/27/22 5

COMPS258 Assignment 2 (Autumn 2021)
Complete the following two functions that can be used for lists.
(i) This function receives a list of numbers and return the sum of only the positive numbers. The function should return None if there is no positive number. Assume that the parameter numlist is a list.
def sumPos(numlist)
(ii) This function returns True if any number in the list parameter check is larger than any number in the list parameter numlist. Assume that both parameters are lists.
def checkLarger(numlist, check)
Submit your solution containing the functions with a suitable main test program in q1f.py. [8 marks]
A program is read by computers (Python interpreters) and programmers, and sometimes many programmers. It is important to write programs that are readable. A readable program is easy to understand by a human.
It is a bad practice to use meaningless identifier like single character as variable name. It is even worse to have too many identical identifiers as both global variable outside a function definition and local variable inside a function definition, but they have entirely different meaning.
A student has submitted a program using a lot of unclear variable identifiers. Please
help the teachers to understand this program.
1 2 3 4 5 6 7 8 9
(i) Is there any error when executing this program? If there is no error, write down the output of the program
(ii) Study line 6. The identifier a is used as the name of a function. The same identifier a is also used as a local parameter variable. Explain why it is not a problem.
s = “message”
def a(a, b, s):
for a in range (3):
return a + b + c + s
def b(a, b, s):
return a + b + c + s
print (a(1, 2, 3))
print (b(1, 2, 3))
2:47 PM 1/27/22 6

COMPS258 Assignment 2 (Autumn 2021)
(iii) Study line 7. what is the purpose of putting global in front of the variable c? What will be the difference in situation of the variable c if the keyword global is not exist like the situation in line 16?
Submit q1g.txt. [4 marks]
Make reference to the same program as in the previous part.
State the scope (using line numbers) of the following variables. The scope may be made up of disconnected regions. State whether the variable is a global variable or local variable.
For example, the variable c at line 3 has scope from 4 to 21 except 16 and 17 and it is a local variable.
(i) The variable c at line 7
(ii) The function a() at line 6
(iii) The variable a at line 6
(iv) The variable a at line 8
Submit your solution in q1h.txt. [4 marks]
2:47 PM 1/27/22 7

COMPS258 Assignment 2 (Autumn 2021)
Question 2 Middle Level Programming (28%) Specific Requirements of Question
Error Tolerance Efficiency
The code that you write should handle erroneous input and parameters and report an error if necessary.
You should avoid overly inefficient coding in your programs.
Some people think the company name is most important in setting up a new business. According to the famous fortune teller Dr. Seven, a good fortune company name should satisfy the following conditions.
􏰀 Has more good letters than bad letters.
􏰀 Has no evil letters.
The definitions are given below.
Evil letters Bad letters Good letters
All evil letters and B A D Other letters
Assume that the company name contains only uppercase letters and spaces. Complete the following function that returns True if it is good fortune company name, and False if otherwise. If the parameter name is not a string, return None.
def isGoodFortune(name)
Submit your solution in q2a.py. [12 marks]
[Challenging] Write a complete program that generates K number of Mark 6 entries of N numbers, where all the numbers appear at most once.
You do not bet on Mark 6? You may read the following specification instead.
Write a program that generates K lists of N random numbers in the range 1 to 49, and none of the drawn random numbers appears more than once.
K and N comes from the input, where 1 <= K <= 6 and 6 <= N <= 8. The following shows an example execution of the program. Note that the 24 numbers in the 4 entries appear only once. The CC and the tutors may provide explanation of the problem but will not provide any further hint to this question, as it is designed for challenge. No mark may be given if the programming solution is not completely correct. Include input validation and re-entry to check the range of N and K but assume that the input is an integer. Submit your programming solution in q2b.py. [12 marks] Enter how many numbers in each entry (N): 6 Enter how many entries (K): 4 The 4 entries generated: [42, 31, 29, 18, 25, 14] [28, 26, 43, 39, 7, 15] [45, 5, 8, 23, 35, 21] [40, 48, 30, 13, 46, 34] (c) This is not a real question. You will earn these 4 marks only if your submission is made correctly. Please refer to the document Instruction on Electronic Submission for the details. [4 marks] 2:47 PM 1/27/22 8 COMPS258 Assignment 2 (Autumn 2021) Question 3 Challenging Programming (12%) Error Tolerance Efficiency Your solution 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. Please submit the whole program In this question we will develop an interesting business analysis model step-by-step. Through this question, you will study how to do business analytics with just loop structures. A company called MetroMini has noticed that a minibus route between the university in Homantin and HungHom railway station should attract lots of passengers. You are recruited as the "business intelligence analyst" for MetroMini. The following lists the operation data for the minibus route. 􏰀 Each minibus can carry 19 passengers, 􏰀 The fuel cost for each one-way trip is $27 􏰀 The daily wage for a driver is $620 􏰀 The daily depreciation cost for a minibus is $264 􏰀 The average daily maintenance cost for a minibus is $35 􏰀 The driver works 8 hours per day 􏰀 Each single trip takes 30 minutes that includes travelling time, passengers boarding and aligning time, waiting time at the terminals and the rest time for the driver. 􏰀 The passenger arriving at the minibus terminal in a random manner throughout the 8 hours of operating time 􏰀 The fare for each passenger for a single trip is $7.5 􏰀 The minibus always wait till the vehicle is full before its departures from the One problem the business development team is now facing is to determine how many minibuses should be allocated each day. Arrange too many minibuses will cause a loss in operation cost. Arrange too few will not handle the passenger needs which also means loss of business. The business team wishes to make as much profit as possible. Work on each part of the business model one step at a time. Submit all parts in a single file as q3a.py. [12 marks] The first step involves modelling the number of passengers sold in one day. Clearly there is a fluctuation from one day to the next, but according to past statistics, an estimation can be made. Complete the following definition of the function that randomly returns one of the ten numbers according to probabilities shown in the following list. def randPassenger() The ten numbers and the probabilities of their generation are given in the table below. Passengers Probabilities 1000 0.01 1500 0.02 2:47 PM 1/27/22 COMPS258 Assignment 2 (Autumn 2021) 2000 0.08 2500 0.13 3000 0.18 3500 0.21 4000 0.21 4500 0.10 5000 0.04 5500 0.02 According to the above table, the function should have the probability of 0.08 (8 in 100 chance) of returning 2000 (selling 2000 passengers in a day), and the probability of 0.21 (21 in 100 chance) returning 3500, etc. Complete the following function definition that calculates (and returns) the profit (or loss) made by the company by simulating the number of passengers available in one day. def simulateOneDay(numOfMinibus) Remember the profit is related to how many passengers are available and whether the number of minibuses arranged on each day is just right. Clearly, allocate the number of minibuses just enough for the number of passenger available each day will maximize the profit. When the previous function randPassenger is called, the function will generate the number of passengers available for simulating one day's business. This function simulateOneDay should simulate the business of a particular day by performing the following: 􏰀 Call the function randPassenger to obtain the number of passengers available 􏰀 Use the parameter numOfMinibus to represent the number of passengers available. 􏰀 Use the cost information given in the question, calculate and return the profit (or loss) in one day. The profit (or loss) will change if you run the test function multiple times. This is due to the number returned from randPassenger can be different. The following two examples illustrate sample return values of function simulateOneDay. Available passengers Number of minibuses allocated Actual passenger carried Return value of simulateOneDay() Meaning of return value 608 (because 1 minibus travel 16 trips a day and each trip can carry 19 passengers) (608 * $7.5) - (($620 + $264 + $35) * 2 + (32 * $27)) = $1858 (1000 & $7.5) – (($620 + $264 + $35) * 9 + (53 * $27)) = 2:47 PM 1/27/22 10 The operation cost and the income are calculated as illustrated below. 𝐶𝑜𝑠𝑡𝑑𝑎𝑦 = 𝑑𝑎𝑖𝑙𝑦 𝑓𝑖𝑥𝑒𝑑 𝑐𝑜𝑠𝑡 𝑝𝑒𝑟 𝑏𝑢𝑠 × 𝑛𝑢𝑚 𝑜𝑓 𝑏𝑢𝑠𝑒𝑠 + 𝑓𝑢𝑒𝑙 𝑐𝑜𝑠𝑡𝑝𝑒𝑟 𝑡𝑟𝑖𝑝 × 𝑛𝑢𝑚 𝑜𝑓 𝑡𝑟𝑖𝑝𝑠 𝐼𝑛𝑐𝑜𝑚𝑒𝑑𝑎𝑦 = 𝑎𝑐𝑡𝑢𝑎𝑙 𝑝𝑎𝑠𝑠𝑒𝑛𝑔𝑒𝑟𝑠 𝑜𝑛 𝑏𝑢𝑠𝑒𝑠 × $7.5 Complete the following function that finds out how many minibuses should be allocated so that the profit is maximized. def findMaxProfit(): The function finds out the answer by trying out different number of minibuses arranged from 1, 2, 3, until 30. For each number of minibus (say 1), the function calls simulateOneDay 100000 times to find out the average profit. Calling 100000 times means simulating the business for 100000 days. You are expected to write two nested for loops. The outer for loop controls the number of minibuses to operate for one day, and the inner for loop controls the simulation of 100000 times. The function should print the average profit for each possible number of minibus (from 1 vehicle to 30 vehicles). Finally, the function should print the number of minibuses that can maximize the average profit. 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com