- | Page
WGU C960 DISCRETE MA TH 2,
QUESTIONS AND ANSWER S, 100%
ACCURATE, VERIFIED.
Question :
Consider the following pseudocode that merges two lists
of numbers into one:
Merge0(List1, List2) Set OUTlist to empty While List1 is not empty OR List2 is not empty If one list is empty and the other is not, Remove the first number from the non-empty list and add it to OUTlist If both lists are non-empty, Remove the first number from List1 and add it to OUTlist Remove the first number from List2 and add it to OUTlist Return OUTlist
If ListA is [1, 3, 5] and ListB is [2, 4, 6] then what is the result of Merge0 (ListA, Merge0 (ListB, ListA))?
- | Page
CORRECT ANSWER: [ 1, 2, 3, 1, 5, 4, 3, 6, 5 ]
Step 1:
To solve this, we need to understand how the Merge0 function works. It merges two lists by taking the first element of each list and adding it to the output list in ascending order. If one list is empty and the other is not, it removes the first element from the non-empty list and adds it to the output list.
Using the given example, we have:
ListA = [1, 3, 5] ListB = [2, 4, 6] Now, let's analyze the innermost function Merge0(ListB,
ListA):
OUTlist = empty
OUTlist = [2]
OUTlist = [2, 1]
- | Page
OUTlist = [2, 1, 4]
OUTlist = [2, 1, 4, 3]
OUTlist = [2, 1, 4, 3, 6]
OUTlist = [2, 1, 4, 3, 6, 5]
ListA is now empty, so we return OUTlist = [2, 1, 4, 3, 6, 5]
Step 2:
Now, let's use the result of Merge0(ListB, ListA) as the second argument for the outer Merge0 function. So we
have:
List1 = ListA = [1, 3, 5] List2 = Merge0(ListB, ListA) = [2, 1, 4, 3, 6, 5]
Using the Merge0 function with these lists, we have:
OUTlist = empty
- | Page
OUTlist = [1]
OUTlist = [1, 2]
OUTlist = [1, 2, 3]
OUTlist = [1, 2, 3, 1]
OUTlist = [1, 2, 3, 1, 5]
OUTlist = [1, 2, 3, 1, 5, 4]
OUTlist = [1, 2, 3, 1, 5, 4, 3]
OUTlist = [1, 2, 3, 1, 5, 4, 3, 6]
OUTlist = [1, 2, 3, 1, 5, 4, 3, 6, 5]