CSCA48 Exercise 3 Due: June 9, 2018. 5:00 pm In this exercise you will be practi
ID: 3893735 • Letter: C
Question
CSCA48 Exercise 3 Due: June 9, 2018. 5:00 pm In this exercise you will be practicing working with double linked lists (dll). You are required to write two functions for which you need to import the DNode and DoubleLinkedList class that was implemented in the lecture (i.e. fromm week4 DLL import ). You can only use the methods that are provided in this code and you're not allowed to add any method to it. A) a function called reverse_merge that takes two double linked lists as its input parameters. The first linked list contains the surname of Marzieh's students in CSCA48 sorted ascendingly and the second contains the surname of Nick's students for the same course sorted in a descending order In order to allocate a room for term test 1, we need to merge these two lists to one list that contains all the student's surname sorted ascendingly. Therefore, your function should return a DLL containing all the surnames. B) A function called allocate_room that takes four parameters; a dll containing students' surnames, a string representing room name and number, an int representing the capacity of the room and an int representing the index of the node that contains the surname of the first person that is assigned to this room. Your job is to return a string that shows the two first letters of the surnames of the first and last person that should attend the given room. Here is an example: >>>allocate_room (csca48_1ist,"SW319", 80, 150) SW319 JU-MO What we know/ don't know We don't know how many students registered for each class therefore you should expect that the length . of these two lists are not the same We know that both Marzieh and Nick implemented their DLLs with a dummy head and tail. csca48_list is also a dll implemented with a dummy head and tail. None of the lists are empty. In fact, before we call these two functions, we check to see if the lists are empty If they are, we don't call the functions. We found that there are a few students who registered for both the lectures therefore their surnames are in two lists. Obviously the resulting list should not repeat their names. It is absolutely fine if the capacity of the room is n and we only have m students, where mExplanation / Answer
def reverse_merge(l1,l2):
L = DoubleLinkedList()
p1 = l1
p2 = l2
while p1 != None and p2 != None:
if p1.get_element() <= p2.get_element():
L.add_last(p1.get_element()
p1 = p1.get_next()
else:
L.add_last(p2.get_element()
p2 = p2.get_next()
while p1 != None:
L.add_last(p1.get_element()
p1 = p1.get_next()
while p2 != None:
L.add_last(p2.get_element()
p2 = p2.get_next()
return L
def allocate_room(list,name,cap,index):
count = 0
p = list
while p!=None and count < index:
p = p.get_next()
count = count + 1
if p == None:
return None
str1 = p.get_element()
count = 0
str2 = ""
while p.get_Next()!=None and count < cap:
p = p.get_next()
count = count + 1
str2 = p.get_element()
return name + " " + str1 + "-" + str2