Exam Computer science notes, Transcriptions of Computer science

Exam Computer science notes with comments and answer

Typology: Transcriptions

2023/2024

Uploaded on 12/17/2024

fahad-14
fahad-14 🇬🇧

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
(c) The following pseudocode subroutine merges two sorted lists of equal or unequal lengths. It
is called with the two lists shown in the main program.
NOTE: Given that list1 = [1, 2, 5, 10], then list1[1:] = [2, 5, 10].
That is, it returns a sublist starting with list1[1].
1. SUB merge (list1,list2)
###The code bellow is for when there is an empty list###
2. IF length(list1) = 0 and length(list2) = 0
3. return []
4. ENDIF
5. IF length (list1) = 0
6. return list2
7. ENDIF
8. IF length (list2) = 0
9. return list1
10. ENDIF
###if first value of list1 is less than first value of list
2 then
Return (list 1’s first index and the truncated version
(remove first index) of list 1 and full of list 2)
Then since it calls the function merge inside the function
merge (recursive function)
You then take your return output and start from the begging
of the function###
11. IF list1[0] <= list2[0]
12. return [list1[0]] + merge(list1[1:], list2)
13. ENDIF
pf3
pf4

Partial preview of the text

Download Exam Computer science notes and more Transcriptions Computer science in PDF only on Docsity!

(c) The following pseudocode subroutine merges two sorted lists of equal or unequal lengths. It is called with the two lists shown in the main program. NOTE: Given that list1 = [1, 2, 5, 10], then list1[1:] = [2, 5, 10]. That is, it returns a sublist starting with list1[1].

  1. SUB merge (list1,list2) ###The code bellow is for when there is an empty list###
  2. IF length(list1) = 0 and length(list2) = 0
  3. return []
  4. ENDIF
  5. IF length (list1) = 0
  6. return list
  7. ENDIF
  8. IF length (list2) = 0
  9. return list
  10. ENDIF ###if first value of list1 is less than first value of list 2 then Return (list 1’s first index and the truncated version (remove first index) of list 1 and full of list 2) Then since it calls the function merge inside the function merge (recursive function) You then take your return output and start from the begging of the function###
  11. IF list1[0] <= list2[0]
  12. return [list1[0]] + merge(list1[1:], list2)
  13. ENDIF

--Same as the comment above but you switch the numbers--- ###Condition for the if statement### If the first position of list2 is less than list 1 first index then Return (list 2’s first index and the truncated version (remove first index) of list 2 and full of list 1) Then since it calls the function merge inside the function merge (recursive function) You then take your return output and start from the begging of the function###

  1. IF list2[0] <= list1[0]
  2. return [list2[0]] + merge(list1, list2[1:])
  3. ENDIF
  4. ENDSUB #End of the whole function This is the main program where you find the parameters for the function
  5. #main program
  6. list1 = [3, 7, 9]
  7. list2 = [1, 2, 5, 10]
  8. mergedList = merge(list1,list2)
  9. print(mergedList) return 1 list 2 = [2,5,10] return 2 list 2 [5,10]

(iii) What parameters will be passed to the subroutine the first, second and third times that a recursive call is made? [3] First time: [3,7,9], [2, 5, 10] Second time: [7,9], [5,10] Third time: [7,9], [10]