2023 RI P2 Question Paper
Uploaded by Kozak327 · 8 September 2026
Preview
Text from the first pages© RI2023 [Turn over CANDIDATE NAME CLASS 23 COMPUTING 9569/02 Paper 2 (Lab-based) 3 hours Additional materials: Electronic version of RECIPES.txt data file Electronic version of CLIENT_TASK2.py file Electronic version of GAMERS.txt file Electronic version of FITNESS.db file Insert Quick Reference Guide READ THESE INSTRUCTIONS FIRST Answer all questions. 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. Approved calculators are allowed. Save each task as it is completed. 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 task. The total number of marks for this paper is 100. FOR EXAMINER’S USE Task 1 Task 2 Task 3 Task 4 TOTAL 100 This document consists of 12 printed pages. RAFFLES INSTITUTION Mathematics Department RAFFLES INSTITUTION 2023 YEAR 6 PRELIMINARY EXAM
2 H2 COMP 9569/2023 RI Year 6 Prelim Exam Instructions to candidates: Your program code and output for each of Task 1 to 3 should be saved in a single .ipynb file using Jupyter Notebook . For example, your program code and output for Task 1 should be saved as: TASK1_<your name>_<index number>.ipynb Make sure that each of your .ipynb files shows the required output in Jupyter Notebook. 1 Name your Jupyter Notebook as: TASK1_<your name>_<index number>.ipynb You are tasked with analysing a collection of bakery recipes. The recipe data is stored in a file named RECIPES.TXT. For each recipe, the name of the bakery item will be listed first, followed by information about each ingredient for that item separated by commas: <ingredient name>,<quantity required>,<unit of measurement> For example, the first recipe in the file is for the Cheesecake item: Cheesecake cream cheese,450,g sugar,150,g eggs,3,pieces sour cream,120,ml vanilla extract,1,tsp flour,30,g The recipes are separated by blank lines in the file RECIPES.TXT. For each of the sub-tasks, add a comment statement at the beginning of the code using the hash symbol ‘#’ to indicate the sub -task the program code belongs to, for example: In [1]: #Task 1.1 Program code Output:
3 H2 COMP 9569/2023 RI Year 6 Prelim Exam [Turn over Task 1.1 Write program code for a function named task1_1 that takes the filename as a parameter and finds the most common ingredient and returns the name of the most common ingredient in the recipes, along with the number of occurrences. [5] Run your program and show the output. [1] Task 1.2 Write program code for an insertion sort function named task1_2 that takes a list of strings, sorts it in ascending alphabetical order, and returns the sorted list. [4] Task 1.3 Write program code for a function named task1_3 that reads the data from the file and calculates the total quantity of each ingredient required across all recipes. If the unit of measurement for an ingredient is the same in multiple recipes, sum up the quantities. Sort the names of ingredients alphabetically using the insertion sort function from Task 1.2 and display the sorted list of ingredients, along with their total quantities and units. [6] Run your program and show the output. [1] Save your Jupyter Notebook for Task 1.
4 H2 COMP 9569/2023 RI Year 6 Prelim Exam 2 Name your Jupyter Notebook as: TASK2_<your name>_<index number>.ipynb A chatterbot is a computer program that responds to characters input and produces a response for each line of input as follows: • The first time that it reads in a line containing the string “hello”, it should generate as a response the output line “Hi, how are you?”. • The second and subsequent times that it reads in a line containing the string “hello”, it should generate as a response the output line “ Hello again, welcome back!”. • When it reads in a line containing the string “thanks” or the string “thank you”, it should generate as a response the output line “You are most welcome.” • When it reads in a line beginning with a string of the form “I <xYz> you”, where <xYz> is any string, it should generate as a response an output line of the form (follow the same casing) “You <xYz> me? I really <xYz> you too.”. • When it reads in a line not containing any of the strings described above, it should generate as a response the output line “Sorry, I do not understand...”. The chatterbot should check each line for the specified strings in the order listed above and generate only one response per input line. For example, the input line “I thank you” should generate as a response only the output line “You are most welcome.”, and it should not generate as a response the output line “You thank me? I really thank you too.”. The chatterbot should not be case sensitive. In other words, it must recognise not only “hello” but also “Hello”, “HELLO”, “hElLo”, etc. The task is to write a chatterbot program to work as a server -client application using sockets. The chatterbot server will listen for incoming connections from client s, and clients will send text -based messages to the server. The server will respond to the client based on the input. You are provided with the client program code that establishes a connection to the server and sends user input. Your task is to create the server program as follows: • Write a function process_input function to process user input and generate responses based on the input rules described above. The function will take a parameter input_text and process the logic and return a respond based on the parameter. • Write the main function for the server program to establish the socket connection, handle communication with client, and close the connection properly.
5 H2 COMP 9569/2023 RI Year 6 Prelim Exam [Turn over The server program should process client requests and respond with the appropriate chatterbot responses. When a client is connected to the server, the server should print this message on its side: “Chatterbot server is listening.” The server should output the conversation with the client on its side, using the labels “Client” and “Chatterbot” to identify the message source. For example, the output on server’s side should show: Client: Hello there! Chatterbot: Hi, how are you? The client should also output the conversation using the labels “ You” and “Chatterbot”, for example, the output on the client’s side should show: You: Hello there! Chatterbot: Hi, how are you? The server will terminate the program when the client enters “ exit”, with a suitable message. Task 2.1 Create the Python code for the server program. [13] Name your server program as: TASK2_<your name>_<index number>.ipynb Test the program by inputting the following lines in client program: [2] This is a test Well, hello there! I am fine, thank you. Wait, I have to go... Never mind. Thanks. HELLO! I have returned. I missed you so much! I thank you... hello? You and I think alike, you know... exit Save your Jupyter Notebook for Task 2.
6 H2 COMP 9569/2023 RI Year 6 Prelim Exam Client Program in CLIENT_TASK2.py: import socket def main(): client_socket = socket.socket() client_socket.connect(("127.0.0.1", 12345)) print("Connected to Chatterbot server.") while True: user_input = input("You: ") client_socket.send(user_input.encode()+b'\n') if user_input.lower() == "exit":
Content continues in the PDF. Download PDF
Related notes
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- 2021 ACJC Computing Promo Paper 2Exam Papers · 2021
- 1992 AJC Computing QPExam Papers · 1992
- 2023 YIJC P2 Question PaperExam Papers · 2023
- 2023 YIJC P1 Question PaperExam Papers · 2023
- 2023 RVHS P2 CombinedExam Papers · 2023
- 2023 RI P1 Question PaperExam Papers · 2023
- 2023 NYJC-VJC-TJC P2 Question PaperExam Papers · 2023
- 2023 NJC P2 Question PaperExam Papers · 2023
- 2023 JPJC P2 SolutionsExam Papers · 2023
- See all H2 Computing notes

