2020 ACJC Prelim Paper 2
2020 ACJC Prelim Paper 2
Higher 2
COMPUTING 9569/02
Paper 2 (Lab-based) 18 August 2020
3 hours
All tasks must be done in the computer laboratory. You are not allowed to bring in or take out
any pieces of work or materials on paper or electronic media or in any other form.
The use of built-in functions, where appropriate, is allowed for this paper unless stated
otherwise.
Note that up to 6 marks out of 100 will be awarded for the use of common coding standards
for programming style.
The number of marks is given in brackets [ ] at the end of each question or part question.
The total number of marks for this paper is 100.
__________________________________________________________________________________
This document consists of 9 printed pages and 1 blank page.
[Turn Over
2
Instruction to candidates:
Your program code and output for each of Task 1 and 2 should be downloaded in a single .ipynb file.
For example, your program code and output for Task 1 should be downloaded as
TASK1_<your name>_<centre number>_<index number>.ipynb
The players have to collect food items. A food item has the following attributes:
• name : STRING
• value : INTEGER
• get_name()
• get_value()
A player takes on the role of a person. A person has the following attributes:
• name : STRING
• health : INTEGER which is initialised at a value of 100
• strength : INTEGER which is initialised at a value of 100
• get_name()
• get_health()
• get_strength()
• eat(food) adds the value of the food to the strength. The code should display the player’s
new strength.
• attack(opponent)
There are two subclasses of the Person class – Healer and the Warrior.
• heal(patient)
Warrior’s attack method is twice as effective, meaning that if the player has enough strength
to attack, opponent’s health is decreased by 2*r, while the player’s strength is decreased
by r.
Task 1.1
Task 1.2
The code should display appropriate messages about the outcome of attack, including the new value
of opponent’s health. [10]
Task 1.3
Use appropriate inheritance to write program code to define the class Healer.
The code should display appropriate messages about the outcome of heal, including the new value of
patient’s health. [4]
Task 1.4
Use appropriate inheritance and polymorphism to write program code to define the class Warrior. [2]
2 An n × n chessboard consists of an n × n grid of small squares. For this task, the squares are
numbered using a coordinate system as a tuple of two integers. The first integer is the column
number, starting from 1 at the left, and the second integer is the row number, starting from 1 at
the bottom.
A chess knight is a piece that occupies a square, and can then move to another square according
to one of the following rules:
• moving two squares horizontally and then one square vertically in either direction, or
• moving two squares vertically and then one square horizontally in either direction.
Only the starting and ending squares of the knight’s move are counted as being visited by the
knight, and not the squares that the knight passes over while moving.
A knight’s tour is a sequence of moves that a chess knight makes on a chessboard, so that it
visits every square of the chessboard exactly once. It does not need to return to its starting square.
Task 2.1
For a given value of n and a list of squares, write program code to determine if the list is a knight’s tour
of an n × n chessboard.
Test your code using the values n=7 and the list of squares given in the files:
• TASK2TOUR.txt, which is a valid tour;
• TASK2NOTOUR.txt, which is not a valid tour. [10]
An algorithm to generate a knight’s tour needs to keep track of the squares already visited, so that the
knight does not visit them a second time during the tour.
Task 2.2
Suppose the knight is currently on square square, and a list of squares already visited by the knight is
given in lis.
Write a function available(square,lis) that returns a list of squares which the knight can visit on
its next move from square, and are not currently in lis. These are the squares available to the knight
as it tries to complete the tour.
The output should be given in lexicographic order, that is, with the column numbers in ascending order,
and, among the squares with the same column number, with the row numbers in ascending order. [6]
Task 2.3
From each square, the knight moves to the available square from which it has the smallest number of
available squares after that. If there is a tie, the square which comes first in lexicographic order is
chosen.
Write program code to generate the knight’s tour as a list of squares in the order they are visited.
3 A text file INVENTORY.txt contains the inventory data for a certain electronics store. Each line
in the file contains tab-delimited data that shows the product name, product type, purchase price,
selling price and quantity available.
Name\tType\tPurchase_Price\tSelling_Price\tQuantity
Task 3.1
Task 3.2
The profit margin of each product can be calculated by the following equation:
• Calculate and display the total profit the store could make if all products are sold;
• Sort the inventory data using a Merge sort algorithm in descending order of profit margin;
• Display the sorted inventory data in the format given below.
Task 3.3
Serial_No\tName\tType\tPurchase_Price\tSelling_Price\tQuantity
Write program code to insert the data from INVENTORY_SERIAL.txt into a NoSQL database
OUTLETS under the collection GEM.
Task 3.4
The database administrator wants to validate that the store manager did not make any errors when he
edited the text file. Write program code to check that the database conforms to the below specifications:
• Serial_No consists of one digit followed by two letters, followed by one digit (e.g. 1AB7);
• Name consists of only letters, digits and spaces;
• Quantity is a positive integer.
Any document that has an error should be removed from the database. You may assume data fields
not specified above are error free. Display the documents that were removed.
4 A company keeps records of the employees working for it. The following are the information stored
in the company’s database:
Employee
Sales
Tech_support
Task 4.1
Task 4.2
The files SALES.txt and TECH_SUPPORT.txt contain information regarding the sales and tech
support employees respectively. The information should be inserted into the database.
Write a python program to insert all information from the two files into the records database,
records.db. Run the program.
Task 4.3
The company wants to filter the employees by Service_status and display the results in a web
browser.
Write a Python program and the necessary files to create a web application that:
Run the web application. Save the output of the program when 'TRUE' is entered as the
Service_status as TASK4_3_<your name>_<centre number>_<index number>.html. [12]