(Solved) : Question 1 10 Points Purpose Work Concept References Bit Carefully Degree Difficulty Easy Q43927630 . . .







Question 1 (10 points): Purpose: To work with the concept of references a bit more carefully. Degree of Difficulty: Easy. If you understand references very well, this is not difficult. In class land in the readings) we saw a version of Selection sort. As described in the readings, our imple- mentation of selection sort works by repeatedly removing the smallest value from an unsorted list and adding it to end of a sorted list until there are no more values left in the unsorted list (see Chapter 1 of the readinas). unsorted – [3, sorted – list() 2. 5. 7. 6. 8. 0. 1. 2. 8. 2] 000 OWN while len(unsorted) > 0: out – min (unsorted) unsorted.remove(out) sorted.append(out) print (sorted) One problem with this implementation is that it modifies the original list (line 6). Unless we know for sure that a list will never be needed in the future, removing all contents is a bit drastic. To address this problem, we can change the code so that a copy of the original list is made first. Since we make the copy, we can say for sure that the copy will never be needed in the future, and so removing all its contents is absolutely fine. which is very The file a2q1.py is available on Moodle, and it contains a function called selection_sort similar to the above code: def selection_sort (unsorted): Returns a list with the same values as unsorted. but reorganized to be in increasing order. :paran unsorted: a list of comparable data values :return: a sorted list of the data values result – list() # TODO use one of the copy() functions here while len(acopy) > 0: out – min(acopy) acopy.remove(out) result.append(out) return result On line 11, there is a TODO item, which is where wewill add code to create a copy the original unsorted list. Also in the file are 5 different functions whose intended behaviour is to copy a list. Your job in this question is to determine which, if any, of these functions does the job right. Note: There is a Python list method called copy . which we are not using on purpose. We need to un- derstand references, and we must not side-step the issue. Page 2 Task For each of the 5 copy functions in the file a291-py, do the following: • Determine if the function makes a copy of the list. Hint: some do not! . Determine if the function is suitable for use in selection_sort. Hint: some are not! Do not change the function selection_sort except to make use of one of the versions of the copy) function on line 11 Do not change the code for any of the copy functions. What to Hand In Your answers to the above questions in a text file called a2q1.txt (PDF, rf, docx or doc are acceptable) You might use the following format: Question 1 copy1) – makes a copy – is suitable copy2) – takes a copy – is not suitable The above example does not necessarily reflect the right answers! Be sure to include your name, NSID. student number, section number and laboratory section at the top of all documents Evaluation Each of the copy functions is worth 2 marks. Your answers to both questions have to be correct to get the marks def copy1(data): Returns a copy of the given list of data Preconditions: param data: a list Return: a copy of the given data copied = data return copied def copy2(data): Returns a copy of the given list of data Preconditions: param data: a list Return: a copy of the given data copied = [1 for d in data: copied.append(d) return copied e def copy3(data, copy): Copies the given List of data. Preconditions: param data: a list param copy: a list with the same contents as data :return: None for din data: copy.append(d) data.remove(d) def copy4(data, copy): Copies the given List of data. Preconditions: :param data: a list param copy: an empty list Post-conditions: copy has the same contents as data :return: None for din data: copy.append(d) def copy5(data): Returns a copy of the given List of data Preconditions: param data: a list Return: a copy of the given data copied – [] for din data: copied += [d] return copied e def selection_sort(unsorted): Returns a list with the same values as unsorted, but reorganized to be in increasing order. param unsorted: a list of comparable data values return: a sorted List of the data values result = list() # TODO use one of the copy() functions here TUDUU3L DILU) LIC COPY ICELUN TIL while len(acopy) > 0: out = min(acopy) acopy.remove(out) result.append(out) return result Show transcribed image text Question 1 (10 points): Purpose: To work with the concept of references a bit more carefully. Degree of Difficulty: Easy. If you understand references very well, this is not difficult. In class land in the readings) we saw a version of Selection sort. As described in the readings, our imple- mentation of selection sort works by repeatedly removing the smallest value from an unsorted list and adding it to end of a sorted list until there are no more values left in the unsorted list (see Chapter 1 of the readinas). unsorted – [3, sorted – list() 2. 5. 7. 6. 8. 0. 1. 2. 8. 2] 000 OWN while len(unsorted) > 0: out – min (unsorted) unsorted.remove(out) sorted.append(out) print (sorted) One problem with this implementation is that it modifies the original list (line 6). Unless we know for sure that a list will never be needed in the future, removing all contents is a bit drastic. To address this problem, we can change the code so that a copy of the original list is made first. Since we make the copy, we can say for sure that the copy will never be needed in the future, and so removing all its contents is absolutely fine. which is very The file a2q1.py is available on Moodle, and it contains a function called selection_sort similar to the above code: def selection_sort (unsorted): Returns a list with the same values as unsorted. but reorganized to be in increasing order. :paran unsorted: a list of comparable data values :return: a sorted list of the data values result – list() # TODO use one of the copy() functions here while len(acopy) > 0: out – min(acopy) acopy.remove(out) result.append(out) return result
On line 11, there is a TODO item, which is where wewill add code to create a copy the original unsorted list. Also in the file are 5 different functions whose intended behaviour is to copy a list. Your job in this question is to determine which, if any, of these functions does the job right. Note: There is a Python list method called copy . which we are not using on purpose. We need to un- derstand references, and we must not side-step the issue. Page 2
Task For each of the 5 copy functions in the file a291-py, do the following: • Determine if the function makes a copy of the list. Hint: some do not! . Determine if the function is suitable for use in selection_sort. Hint: some are not! Do not change the function selection_sort except to make use of one of the versions of the copy) function on line 11 Do not change the code for any of the copy functions. What to Hand In Your answers to the above questions in a text file called a2q1.txt (PDF, rf, docx or doc are acceptable) You might use the following format: Question 1 copy1) – makes a copy – is suitable copy2) – takes a copy – is not suitable The above example does not necessarily reflect the right answers! Be sure to include your name, NSID. student number, section number and laboratory section at the top of all documents Evaluation Each of the copy functions is worth 2 marks. Your answers to both questions have to be correct to get the marks
def copy1(data): Returns a copy of the given list of data Preconditions: param data: a list Return: a copy of the given data copied = data return copied def copy2(data): Returns a copy of the given list of data Preconditions: param data: a list Return: a copy of the given data copied = [1 for d in data: copied.append(d) return copied e
def copy3(data, copy): Copies the given List of data. Preconditions: param data: a list param copy: a list with the same contents as data :return: None for din data: copy.append(d) data.remove(d) def copy4(data, copy): Copies the given List of data. Preconditions: :param data: a list param copy: an empty list Post-conditions: copy has the same contents as data :return: None for din data: copy.append(d)
def copy5(data): Returns a copy of the given List of data Preconditions: param data: a list Return: a copy of the given data copied – [] for din data: copied += [d] return copied e def selection_sort(unsorted): Returns a list with the same values as unsorted, but reorganized to be in increasing order. param unsorted: a list of comparable data values return: a sorted List of the data values result = list() # TODO use one of the copy() functions here
TUDUU3L DILU) LIC COPY ICELUN TIL while len(acopy) > 0: out = min(acopy) acopy.remove(out) result.append(out) return result
Expert Answer
Answer to Question 1 (10 points): Purpose: To work with the concept of references a bit more carefully. Degree of Difficulty: Easy…
OR