COMPS208F Introduction to Computer Programming
Autumn 2019 Presentation Assignment 1 Please submit this Assignment by 4 December 2019
COMPS208F Assignment (Autumn 2019)
Preamble
This assignment has replaced Quiz 2 (5%) and the Practical Test (10%) for this presentation of COMPS208F, as an exceptional measure for the suspension of on-site teaching and learning activities for the Autumn 2019 term.
Submission
All answers should be submitted in files. Each question specifies the files (i.e.the names of the files) to be submitted. All the files should be combined into a ZIP file. The file name should be OUID-Surname.zip. For example, if you OUID is 12345678 and your surname is Chan, then the file name should be 12345678-Chan.zip.
Please submit the ZIP file to the OLE COMPS208F Assigment 1
Grading of Programming Exercises
A program header, containing at least your name, OUID, date, and the purpose of the program, should be included in each submitted program file. Failure to do so will be penalized.
Submitted programs that are fail to run may get 0 mark.
Programs with poor styles such as redundant logic and statements and poor chosen
variable names may be penalized up to 20% of the allocated mark.
Input validation is not required, unless it is explicitly required by the question.
Submission Summary
The following table shows the files that may be submitted. The submitted files will be computer-processed. Failure to use the correct file names may earn you zero mark.
Filename
Format
Question
q1a.py
Python Source
Question 1(a)
q1b.py
Python Source
Question 1(b)
q1c.py
Python Source
Question 1(c)
q1d.py
Python Source
Question 1(d)
q1e.py
Python Source
Question 1(e)
q1f.py
Python Source
Question 1(f)
q2a.txt
Text Article
Question 2(a)
q2b.py
Python Source
Question 2(b)
All students are expected to attempt Question 1 (q1a to q1g). Question 2 parts are challenging and you should just try as much as you can.
9:31 PM 11/23/19 2
COMPS208F Assignment (Autumn 2019)
Question 1 Programming Exercises (56%)
(a)
Vanilla () is a spice that you often find in processed food. It currently costs around HK$5100 per kilogram. Write a program that calculates the cost of buying a certain weight of fresh vanilla.
The program should read in the weight of vanilla to buy (in kilograms), calculate, and then print out the answer. Follow the format in the example below.
Submit your solution in q1a.py. [12 marks]
Enter weight of vanilla (kilograms): 0.254 The cost is $1295.4
(b)
Write a complete program that prints a square of a certain pattern. The example below shows two executions of the same program.
The program should first read in the size of the square as an integer, and then print the square of the pattern shown below.
Enter size: 5
$000$
$111$
$222$
$333$ $444$
Enter size: 8
$000000$
$111111$
$222222$
$333333$
$444444$
$555555$
$666666$
$777777$
Submit your solution in q1b.py. [12 marks]
(c)
Add input validation to part (b) above. The only acceptable input is a positive integer greater than 0. The program should print an error message and allow the user to enter input again if an input error occurs. The input errors and the corresponding errors messages are summaried below.
Submit your solution in q1c.py. [8 marks]
Input Errors
Error Messages
The input is an empty line
Error! Empty line
The input is not an integer
Error! Not an integer
The input is not an integer
Error! Not a positive integer
9:31 PM 11/23/19 3
COMPS208F Assignment (Autumn 2019)
(d)
Chris is superstitious. He believes that two positive integers A and B are lucky pair if A is greater than B and the product of A and B (i.e. A multiplied by B) is wholly divisible by 5, 23 and 31.
Write a complete program to find out how many such lucky pairs A and B, can be found between 0 and 1000 (inclusive). Submit your solution in q1d.py. [8 marks]
The following are examples of lucky pairs.
Hint: there should be 652 such lucky pairs.
A
B
Remarks
115
31
115 x 31 = 3565
465
437
465 x 437 = 203205
800
713
800 x 713 = 570400
(e)
The solution for the Mark 6 Lottery problem given in the Lecture Notes on page 148 can generate 6 randomly integers for a drawing, which are unique (no duplication) and within the range 1 to 49 (inclusively).
Make the following changes on the solution:
Generate 4 drawings (instead of just 1) of 6 random numbers each, and all drawn numbers should be unique across the 4 drawings. Each of the 4 drawings should also be randomized and sorted.
The range of numbers should be 1 to 53 instead.
The following unlucky numbers should be excluded: 4, 14, 24, 34. The program requires no input. The output should look like the following.
Submit your solution in q1e.py. [8 marks]
[5, 7, 15, 18, 21, 45] [3, 12, 23, 28, 33, 36] [8, 10, 25, 32, 40, 44] [11, 22, 29, 31, 46, 52]
(f)
Write a program with the following list operations:
Create an empty list and assign to a variable named apple.
Append all even integers from 100 to 200 (inclusive) to the list using append method.
Use list comprehension to create a new list from apple. The new list should contain the positive square root of the even integers from 100 to 200. Assign the new list to a variable named orange.
Print the content of the list in orange to the screen.
Add up all the numbers in the orange list and print the sum to the screen. Submit your solution in q1f.py. [8 marks]
9:31 PM 11/23/19 4
COMPS208F Assignment (Autumn 2019)
Question 2 Mini-Projects (44%)
(a)
[Mini Research Project]
This mini research project requires you to find and study additional materials on the Internet. You will then analyze your materials, select your perspective, and write up a brief article of around 300 words (maximum 350 words).
Python programmers have a number of array like data containers at their disposal. Page 166 of the Lecture Notes compares a number of array type Python data containers. Pages 171 to 177 describe the NumPy array. NumPy is a popular module for data scicnce work.
Write a short article that compares the Python list, the NumPy array, and the Python array module. Submit in a file named q2a.txt. [24 marks]
A suggested structure consists of four paragraphs:
Discuss Python list
Discuss Numpy array
Discuss the Python array module
Critically compare these data containers These are some favourable features of a good article.
Correct information
Key informaton selected and irrelevant information discarded
Cohesive in the discussion.
Reveal your thougths, supported with logical arguments
(b)
[Challenging Mini Programming Project]
The Boss Stage of Lab Practical 11 involves the development of a program to predict the winning probability of each team based on the Monte Carlo method. A solution can be found in the skeleton files.
Based on my solution or your solution, attempt the following project objectives: 1. Give proper names to the teams as follows.
Team 0
France
Team 1
Belgium
Team 2
England
Team 3
Croatia
9:31 PM 11/23/19
5
COMPS208F Assignment (Autumn 2019)
2. Change the historical winning ratio between two teams to the following:
Opponents
Team
France
Belgium
England
Croatia
France
0.65
0.45
0.53
Belgium
0.51
0.62
England
0.46
Croatia
* Example: France has a winning probability of 0.65 if the opponent is Belgium (i.e. France is more likely to win).
3. [Challenging] Instead of simulating a knock-out competition, write a Monte-Carlo simulator for a round-robin tourament for 6 national teams. In a round-robin tournament, each team will meet all other teams once.
The Monte-Carlo simulations should happen for 1,000 times. The program should print out the winning probability of each team in a round-robin tournament.
The following table lists the winning ratio between any two teams:
Team
France
Belgium
England
Croatia
Brazil
Argentina
France
0.65
0.45
0.53
0.52
0.45
Belgium
0.51
0.62
0.53
0.40
England
0.46
0.35
0.47
Croatia
0.32
0.50
Brazil
0.51
Argentina
* Example: Croatia has a winning probability of 0.32 if the opponent is Brazil (i.e. Croatia is more likely to lose).
Your solution should require no user input. Submit your solution in q2b.py. [20 marks]
9:31 PM 11/23/19 6
Reviews
There are no reviews yet.