Question: python3.6 In file copy.py, you are provided the following code to return a copy of the list. When I ran this code on lists of
python3.6
In file copy.py, you are provided the following code to return a copy of the list. When I ran this code on lists of size 100, 1000, 10000 I got the following times. >>> copy(mList(100))
5.845847239527302e-05
>>> copy(mList(1000))
0.0028269803253771803
>>> copy(mList(10000))
0.13997457279574732 As you can see, the time is increasing proportional to the square of the length of the list l. Fix my code to make it run linearly in the length of list l.
import time
def mList(i): l = [] for j in range(i): l.append(j) return l
def copy(l): "l: list, return: list" x= time.clock() res = [] for a in l: res = res + [a] print(time.clock() -x) return res // return
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
