1

Make a copy in python

Function below does make a copy of given data.

def copyA(data):
acopy = []
for d in data:
    acopy.append(d)
return acopy

But when I use it in an another function it does return anything. Can anybody tell me what's wrong and how can I fix it?

def selection_sort(unsorted):
result = list()
acopy=[]
for d in unsorted:
    acopy.append(d)
    return acopy
while len(acopy) > 0:
    out = min(acopy)
    acopy.remove(out)
    result.append(out)
    return result
print(result)

unsorted=[1,2,3,6,4,2,1,6,5] selection_sort(unsorted)

Submitted May 26th 2021 by Admin

Answers
0

The keyword return stops the function it is placed in as soon as it is encountered, hence selection_sort does not reach the while loop. If you delete return acopy it should work as expected

Admin | 3 months ago



Relevant Questions