C960 Discrete Math II
PA (Flashcards with
solutions) | Full
Questions, Correct
Answers, and Worked
Solutions | 2026 Update
| 100% Correct.
,Pre-Assessment: Algorithms - Algorithm [ 1, 2, 3, 1, 5, 4, 3, 6, 5 ]
Structures: Step 1:
Question 1: To solve this, we need to understand how the Merge0
Consider the following pseudocode that function works. It merges two lists by taking the first
merges two lists of numbers into one: element of each list and adding it to the output list in
Merge0(List1, List2) ascending order. If one list is empty and the other is
not,
Set OUTlist to empty it removes the first element from the non-empty list
and While List1 is not empty OR List2 is not adds it to the output list.
empty Using the given example, we
have: If one list is empty and the other is not, ListA = [1, 3, 5]
Remove the first number from the non- ListB = [2, 4, 6]
empty list and add it to OUTlist Now, let's analyze the innermost function
Merge0(ListB, If both lists are non-empty, ListA):
Remove the first number from List1 and 1. OUTlist = empty
add it to OUTlist 2. OUTlist = [2]
Remove the first number from List2 and 3. OUTlist = [2, 1]
add it to OUTlist 4. OUTlist = [2, 1,
4]
Return OUTlist 5. OUTlist = [2, 1, 4, 3]
6. OUTlist = [2, 1, 4, 3, 6]
If ListA is [1, 3, 5] and ListB is [2, 4, 6] 7. OUTlist = [2, 1, 4, 3, 6, 5]
then what is the result of Merge0 (ListA, 8. ListA is now empty, so we return OUTlist = [2, 1, 4,
3, Merge0 (ListB, ListA))? 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:
1. OUTlist = empty
2. OUTlist = [1]
3. OUTlist = [1, 2]
4. OUTlist = [1, 2, 3]
5. OUTlist = [1, 2, 3, 1]
6. OUTlist = [1, 2, 3, 1, 5]
7. OUTlist = [1, 2, 3, 1, 5, 4]
8. OUTlist = [1, 2, 3, 1, 5, 4, 3]
9. OUTlist = [1, 2, 3, 1, 5, 4, 3, 6]
10. OUTlist = [1, 2, 3, 1, 5, 4, 3, 6, 5]
11. return OUTlist = [1, 2, 3, 1, 5, 4, 3, 6, 5]
Therefore, the result of Merge0(ListA, Merge0(ListB,
ListA)) is [1, 2, 3, 1, 5, 4, 3, 6, 5].
,
PA (Flashcards with
solutions) | Full
Questions, Correct
Answers, and Worked
Solutions | 2026 Update
| 100% Correct.
,Pre-Assessment: Algorithms - Algorithm [ 1, 2, 3, 1, 5, 4, 3, 6, 5 ]
Structures: Step 1:
Question 1: To solve this, we need to understand how the Merge0
Consider the following pseudocode that function works. It merges two lists by taking the first
merges two lists of numbers into one: element of each list and adding it to the output list in
Merge0(List1, List2) ascending order. If one list is empty and the other is
not,
Set OUTlist to empty it removes the first element from the non-empty list
and While List1 is not empty OR List2 is not adds it to the output list.
empty Using the given example, we
have: If one list is empty and the other is not, ListA = [1, 3, 5]
Remove the first number from the non- ListB = [2, 4, 6]
empty list and add it to OUTlist Now, let's analyze the innermost function
Merge0(ListB, If both lists are non-empty, ListA):
Remove the first number from List1 and 1. OUTlist = empty
add it to OUTlist 2. OUTlist = [2]
Remove the first number from List2 and 3. OUTlist = [2, 1]
add it to OUTlist 4. OUTlist = [2, 1,
4]
Return OUTlist 5. OUTlist = [2, 1, 4, 3]
6. OUTlist = [2, 1, 4, 3, 6]
If ListA is [1, 3, 5] and ListB is [2, 4, 6] 7. OUTlist = [2, 1, 4, 3, 6, 5]
then what is the result of Merge0 (ListA, 8. ListA is now empty, so we return OUTlist = [2, 1, 4,
3, Merge0 (ListB, ListA))? 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:
1. OUTlist = empty
2. OUTlist = [1]
3. OUTlist = [1, 2]
4. OUTlist = [1, 2, 3]
5. OUTlist = [1, 2, 3, 1]
6. OUTlist = [1, 2, 3, 1, 5]
7. OUTlist = [1, 2, 3, 1, 5, 4]
8. OUTlist = [1, 2, 3, 1, 5, 4, 3]
9. OUTlist = [1, 2, 3, 1, 5, 4, 3, 6]
10. OUTlist = [1, 2, 3, 1, 5, 4, 3, 6, 5]
11. return OUTlist = [1, 2, 3, 1, 5, 4, 3, 6, 5]
Therefore, the result of Merge0(ListA, Merge0(ListB,
ListA)) is [1, 2, 3, 1, 5, 4, 3, 6, 5].
,