W01 Teach - Problem 2 Approach
To solve the List Selector problem, we will need to do the following as we loop through the selector list:
If the current value in the selector list is a 1, then we want to get the next item from the first list
If the current value in the selector list is a 2, then we want to get the next item from the second list
We need to append the value we just selected to the new list
We will need a way to determine what the next item is in either list 1 or list 2. We will need variables to keep track of where we are (i.e. the index) in both lists:
pos_list1
- We will use this to keep track of the index of the next item in the first listpos_list2
- We will use this to keep track of the index of the next item in the second list
We will initialize both of these variables to 0. When we select an item from one of the lists (per the selector list) we will add one to either pos_list1
or pos_list2
depending on which one was selected.
![Shows list1, list2, and a selector_list as described in the example from W01-Teach. Arrows are drawn from the selector_list to either list1 or list2 depending on the value in the selector_list (1 goes to list1 and 2 goes to list2). A table is shown for each loop through the selector list (0 to 7) and the values of pos_list1 (0, 1, 1, 1, 2, 3, 3, 3) and pos_list2 (0, 0, 0, 1, 1, 1, 2, 3) as we go through the list. The building of the result_list based on the pos_list1 and pos_list2 is shown as: [1], [1,2], [1,2,10], [1,2,10,20], [1,2,10,20,3], [1,2,10,20,3,4], [1,2,10,20,3,4,30], [1,2,10,20,3,4,30,40].](merge_lists_plan.jpeg)