Rewrite the function isort by using only explicit for loops

Rewrite the function i_sort by using only explicit for loops. In other words, get rid of the inner while loop and replace it with an explicit statement like for cnt in range(0, len(svals)) You will need to rewrite the function accordingly. def i_sort(vals): svals = [vals[0]] for kk in range(l, len(vals)): cnt = 0 while cnt

Solution

def i_sort(vals):
   svals = [vals[0]]
   for kk in range(1,len(vals)):
       flag = True
       for cnt in range(len(svals)):
           if vals[kk] <= svals[cnt]:
               svals.insert(cnt,vals[kk])
               # if insert set flag to false.
               flag = False
               break
       if flag:
           svals.append(vals[kk])
   return svals
print i_sort([5,4,3,1,2,6])

# sample output
# [1,2,3,4,5,6]

 Rewrite the function i_sort by using only explicit for loops. In other words, get rid of the inner while loop and replace it with an explicit statement like fo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site