A201 Introduction to Programming 1

A201 Homework 9

Due Date: Tuesday, November 7, 2023.

Goal: to use lists in Python.

Make sure to complete Lab 9 before doing the homework.

Create a Python script file called hw9.py. Add your name at the top as a comment, along with the class name and date.

Ex. 1. a. Remove Duplicates

Write a function called copyUnique that makes a copy of a list without any duplicate values without using built-in functions. The function should take one parameter which should be a list, and return a value that is another list. You can refer to the function copyList seen in class for a model.

For this, first create an empty list and store it in a variable. Then for each element in the first list (the parameter), check if this element is in the new list already. If not, then add it to it using the function append. If yes, then ignore it. You can use the operator in to test if the element is in the second list. If x is a value and a is a list, to test if the element is in the list you can do:
if x in a:

Add a main function where you input a list from the user using one of the functions seen in class (you can copy that function into the code first), then call the function copyUnique and print the result. Make a call to this function after its definition.

Test the program a few times to cover different possible cases.

Ex. 2 Tracing Sorting Algorithms

Trace by hand at least 10 steps of each of the two sorting algorithms discussed in class: the bubble sort and the selection sort, on the following list of values:

[12, 2, 3, 18, 9, 14, 8, 10, 7, 15]

You can add this part in the file hw9.py after the call to the main. Mark each group based on which algorithm you traced. Also answer the question: where would the improved bubble sort stop on the same array? After which traversal?

Homework Submission

Upload the files lab9.py and hw9.py in Canvas, Assignments, Homework 9.