Question: In question 4 , you will build a class CourseRegistry that store all the courses in a school. The class will use hash table (

In question 4, you will build a class CourseRegistry that store all the courses in a school. The class will use hash table (backed by Python list) with separate chaining. Python built-in function hash() is used to calculate hash code of a course using the course code as key. The calculated hash code will then map to corresponding index position of the hash table using a modulo operator. The constructor is provided in the Assignment Template.
(a) Method insert() is used to add a course into course registry. Complete the method with the specifications below:
1) Take one input course (type Course). Validate its type. Raise an error and stop the program if the input type is invalid.
2) Create a LLNode object with course.code as key and course as value (the class LLNode is provided in the Assignment Template; do not change the code of the LLNode class and do not use other implementations of linked lists).
3) Determine the hash table index by calling hash(course.code)% self.capacity and insert the LLNode object to the front of the linked list.
4) Increment the size attribute by 1.
(b) Method search() is used to find and return a course from the course registry. Complete the
method with the specifications below:
1) Take one input code (type str). Validate its type. Raise an error and stop the program if the input type is invalid.
2) Determine the hash table index by calling hash(code)% self.capacity.
3) Search along the linked list to see if any matching LLNode with key equals to the inputted code. If found, return the value attribute of the LLNode. Otherwise, return
None.
(c) Run the testing code provided in the Assignment Template. It should produce the following
outputs (excluding the line numbers). Do not alter the testing code.
1. None
2. Data Structures
3.
4. Register Chan TM 22061762A successful.
5. Register CHOW MP 22167034A successful.
6. Register LAM CS 22118617A successful.
7. Register LEUNG WA 22018089A successful.
8. Register LI CC 22134887A successful.
9. Course full. Put LIU MK 22052452A in waiting queue.
10. Course full. Put NG KY 22018110A in waiting queue.
11. Course full. Put SHEK CL 22161668A in waiting queue.
12. Course full. Put TSE David 22158990A in waiting queue.
13. Course full. Put WONG KM 22153656A in waiting queue.
14.
15. Remove CHOW MP 22167034A successful.
16. Register LIU MK 22052452A successful.
17.
18. Course code: SEHH2239
19. Course title: Data Structures
20. Course size: 5/5
21. Registered students:
22.22018089A Name: LEUNG WA, ID: 22018089A
23.22052452A Name: LIU MK, ID: 22052452A
24.22061762A Name: Chan TM, ID: 22061762A
25.22118617A Name: LAM CS, ID: 22118617A
26.22134887A Name: LI CC, ID: 22134887A
27. Students in queue:
28. Queue size: 4/8
29. Name: NG KY, ID: 22018110A
30. Name: SHEK CL, ID: 22161668A
31. Name: TSE David, ID: 22158990A
32. Name: WONG KM, ID: 22153656A
###### Question 4 ###### (do not change this line)
class CourseRegistry(object): # (do not change this line)
def __init__(self, capacity=10): # (do not change this function)
self.capacity = capacity
self.hashtable =[None]* capacity
self.size =0
def insert(self, course): # (do not change this line)
#################################################################
# Question 4(a)
#################################################################
# Start of your code
# End of your code
#################################################################
pass # (do not change this line)
def search(self, code): # (do not change this line)
#################################################################
# Question 4(b)
#################################################################
# Start of your code
# End of your code
#################################################################
pass # (do not change this line)
registry = CourseRegistry()
# Insert 1000 dummy courses to the registry to populate the hash table
for code in range(1000):
course = Course(f"HKCC{code:04d}", "DUMMY", 30)
registry.insert(course)
course1= Course("SEHH2239", "Data Structures", 5)
course2= Course("BHMH2103", "Advertising and Promotion", 10)
registry.insert(course1)
registry.insert(course2)
print(registry.search("SEHH2042"))
sehh2239= registry.search("SEHH2239")
print(sehh2239.title)
print()
slist =[Student('Chan TM','22061762A'), Student('CHOW MP','22167034A'), Student('LAM CS','22118617A'),
Student('LEUNG WA','22018089A'), Student('LI CC','22134887A'), Student('LIU MK','22052452A'),
Student('NG KY','22018110A'), Student('SHEK CL','22161668A'), Student('TSE David','22158990A'),
Student('WONG KM','22153656A')]
for e in slist:
sehh2239.addStudent(e)
print()
sehh2239.removeStudent(slist[1])
print()
sehh2239.PrintCourse()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!