Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

just the copy 3/5 (answers) and explanation plz In the readings we saw a version of Selection sort. As described in the readings, our implementation

image text in transcribedimage text in transcribedimage text in transcribed

just the copy 3/5 (answers) and explanation plz

In the readings we saw a version of Selection sort. As described in the readings, our implementation 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 2 of the readings). 1 unsorted = [3, 2, 5, 7, 6, 8, 0, 1, 2, 8, 2] 2 sorted = list() 3 while len(unsorted) > 0: 5 min (unsorted) 6 unsorted.remove(out) 7 sorted.append(out) 8 9 print (sorted) out 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. The file a3q1.py is available on Moodle, and it contains a function called selection_sort() which is very similar to the above code: 1 def selection_sort (unsorted): 2 3 Returns a list with the same values as unsorted, 4 but reorganized to be in increasing order. 5 :param unsorted: a list of comparable data values 6 :return: a sorted list of the data values 7 8 9 = list) 10 11 one of the copy () functions here 12 13 while len (acopy) > > 0: 14 out = min(acopy) 15 acopy.remove(out) 16 result.append(out) 17 18 result # TODO use return result On line 11, there is a TODO item, which is where we will 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. Task For each of the 5 copy() functions in the file a3q1.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. You will only need one or two lines of code to use the different functions, but exactly how many you need is part of the problem. Note: Some of the doc-strings may be inaccurate or wrong. This is part of the problem. In fact, it's quite common for poor programmers to fix bugs and redesign functions, without correcting the documentation! What to Hand In Your answers to the above questions in a text file called a3q1.txt (PDF, rtf, docx or doc are acceptable). You might use the following format: Question 1 copy1() - makes a copy - is suitable copy2() makes 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 copy3(data): Copies the given list of data. Preconditions: :param data: a list :param copy: a list with the same contents as data Postconditions: data becomes empty :return: A copy of the list copied = [] for i in range(len(data)): d = data[i] data.remove(d) copied.append(d) return copied A liefm.copy5 (datammcopy.li. Copies values from data to the empty list copy Preconditions: :param data: a list Post-conditions: copy has the same contents as data Return: None for din data: copy = copy + [d] return None

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago