Lecture 21 — Exercises

Solutions to the problems below must be sent to Submitty for automatic scoring. A separate file must submitted for each problem. Solutions must be submitted by 4 pm on Tuesday, December 5. Download the file insert_sort_and_merge.zip from the course Piazza site.

  1. The file insert_sort.py contains an implementation of the insertion sort algorithm. It includes print function calls. What are the outputs of these statement? Submit a text file showing the output.

  2. The file merge.py contains an implementation of the merge function that is the heart of merge sort. We only consider merge here and not the full sort. Modify the merge function so that if the same value appears in both lists then only one copy of the value is in the final merged list. You may assume that each contains no duplicates. Work within the context of the merge function itself, meaning that you should not use a set and you should not use an extra list. You need to only change a few lines of code.

    For example, if the two lists are

    L1 = [ 2, 7, 9, 12, 17, 18, 22, 25 ]
    L2 = [ 1, 5, 6, 8, 13, 14, 15, 18, 19, 23, 25 ]
    

    Then the result merge(L1,L2) should be the list

    [ 1, 2, 5, 6, 7, 8, 9, 12, 13, 14, 15, 17, 18, 19, 22, 23, 25 ]
    

    Do not change the name of the function and do not change the name of the file. On Submitty we will run code that imports merge.py and calls the function merge passing two lists as arguments.