2023 JPJC P2 Solutions
Uploaded by Kozak327 · 8 September 2026
Preview
Text from the first pages1 #Task 1.1 def linear_search_outliers(Data, Maximum): outliers = [] for i in range(len(Data)): if Data[i] > Maximum: outliers.append(i) return outliers #Task 1.2 # Copy and paste the code from Task2_2.txt to initialize the data list Data = [ 51.1, 77.3, 82.4, 97.5, 104.6, 69.8, 105.2, 95.7, 62.3, 109.1, 76.9, 81.5, 68.2, 53.9, 59.6, 88.4, 85.0, 55.6, 67.7, 86.3, 89.9, 75.0, 79.2, 52.4, 99.8, 92.1, 92.3, 91.2, 93.7, 103.0, 107.5, 94.6, 60.1, 100.9, 73.5, 103.5, 98.4, 51.6, 78.7, 74.2, 101.4, 106.8, 63.7, 72.8, 87.6, 58.8, 66.4, 56.1, 84.3, 61.9 ] maximum_value = 90.0 outlier_indices = linear_search_outliers(Data, maximum_value) print(outlier_indices) print() filtered_data_list = [] for i in range(len(Data)): if i not in outlier_indices: filtered_data_list.append(Data[i]) print(filtered_data_list)
2 #Task 1.3 def quicksort(Data): if len(Data) > 1: pivot = Data[0] #fisrt item as pivot smaller = [] larger = [] for item in Data[1:]: if item < pivot: smaller.append(item) else: larger.append(item) return quicksort(smaller) + [pivot] + quicksort(larger) else: if len(Data) == 1: return [Data[0]] else: return [] #Task 1.4 sorted_data = quicksort(filtered_data_list) # Display the sorted numerical data print("Sorted Data without Outliers:", sorted_data)
3 #task2.1 def hash_function(ISBN): total = 0 for char in ISBN: total += ord(char) remainder = total % 53 return remainder print(hash_function("0205080057")) #task 2.2 class Book_Record(): def __init__(self,ISBN,Title,Author,Due_Date): self.ISBN = ISBN self.Title = Title self.Author = Author self.Due_Date = Due_Date def Get_ISBN(self): return self.ISBN def Get_Title(self): return self.Title def Get_Author(self): return self.Author def Get_Due_Date(self): return self.Due_Date def Set_Due_Date(self,new_due_date): self.Due_Date = new_due_date
4 def to_string(self): return self.Get_ISBN() +', '+ self.Get_Title() + ', ' + self.Get_Author()+', '+self.Get_Due_Date() #task2.3 hta = [Book_Record('','','','') for i in range(53)] #array that stores up to 53 Book_Record objects file = open("Task2_3.txt",'r') for line in file: line = line.strip().split(',') hash_value = hash_function(line[0]) index = hash_value full = False while hta[index].Get_ISBN() != '': index = (index+1) % len(hta) if index == hash_value: #hash table is full full = True break if full == False: #add to hash table array if not full hta[index] = Book_Record(line[0],line[1],line[2],line[3])
5 #task2.4 def search_book_record(hta): isbn = input("enter ISBN: ") hash_value = hash_function(isbn) index = hash_value while True: if hta[index].Get_ISBN() == isbn: #found return hta[index].to_string() if hta[index].Get_ISBN() == '': #not full and not found return "Book not on loan" index = (index+1) % len(hta) if index == hash_value: #full and not found return "Book not on loan" #task2.5 print(search_book_record(hta)) print(search_book_record(hta))
6 task2.6 def update_book_record(hta): isbn = input("enter ISBN: ") due_date = input("new due date: ") index = hash_function(isbn) while True: if hta[index].Get_ISBN() == isbn: hta[index].Set_Due_Date(due_date) break else: index = (index+1) % len(hta) #Task 2.7 update_book_record(hta) def display(hta): index = 0 print("Index".ljust(8) + "ISBN".ljust(12) + "Title".ljust(40) + "Author".ljust(30) + "Due_Date") for record in hta: if record is not None: print(str(index).ljust(8) + record.Get_ISBN().ljust(12) + record.Get_Title().ljust(40) + record.Get_Author().ljust(30) + record.Get_Due_Date()) else: print(str(index)) index += 1 display(hta)
7 Task 3.1 class Node: # [2] def __init__(self, data): self.data = data self.next = None class Stack: def __init__(self): #[1] self.top = None # pointer to Node object def push(self, data): #[2] temp = self.top self.top = Node(data) self.top.next = temp def pop(self): #[2] temp = self.top self.top = self.top.next return temp def to_string(self): #[3] result = [] curr = self.top while curr!=None: result.append(curr.data) curr = curr.next return ", ".join(result) Output: ship, yacht, train, car, bus, plane ship yacht train
8 # Task 3.2 lst = ['plane','bus','car','train','yacht','ship'] stack = Stack() for ele in lst: stack.push(ele) #1 print(stack.to_string()) #1 print(stack.pop().data) print(stack.pop().data) print(stack.pop().data) #1 # Task 3.3 class Queue: def __init__(self): #1 self.head = None # pointer to Node object def enqueue(self, data): # add to end of queue if self.head == None: #queue is empty self.head = Node(data) # 1 else: # add to end prev = self.head curr = self.head.next while curr!=None: prev = curr curr = curr.next #2 prev.next = Node(data) #1 def dequeue(self): # remove from front of queue temp = self.head
9 self.head = self.head.next return temp #2 def to_string(self): result = [] curr = self.head #1 while curr!=None: result.append(curr.data) curr = curr.next #1 return ", ".join(result) # Task 3.4 lst = ['plane','bus','car','train','yacht','ship'] q = Queue() for ele in lst: q.enqueue(ele) #1 print(q.to_string()) #1 print(q.dequeue().data) print(q.dequeue().data) print(q.dequeue().data) #1 Output: plane, bus, car, train, yacht, ship plane bus car
10 # Task 4.1 #[5] import sqlite3 connection = sqlite3.connect("MerlionThemePark.db") #1 sql = '''CREATE TABLE Ticket ( tDate TEXT PRIMARY KEY, dayOfWeek TEXT, unitPrice INTEGER, totQuan INTEGER, availQuan INTEGER )''' #2 connection.execute(sql) sql2 = '''CREATE TABLE "Sale" ( sID INTEGER PRIMARY KEY AUTOINCREMENT, tDate TEXT, quan INTEGER, totalPrice INTEGER, FOREIGN KEY(tDate) REFERENCES Ticket(tDate) )''' #2 connection.execute(sql2) connection.close() # Task 4.2 #[5] import sqlite3 connection = sqlite3.connect("MerlionThemePark.db") infile = open("TICKET.txt") lines = infile.readlines() #1 for line in lines: line = line.strip().split(',') #1 connection.execute("INSERT INTO Ticket(tDate, dayOfWeek, unitPrice, totQuan, availQuan) " + "VALUES(?,?,?,?,?)", (line[0],line[1],line[2],line[3],line[4])) #2
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 P2 Question PaperExam 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
- See all H2 Computing notes

