insertion - Python "int is not subscriptable" error -
why isn't working? says int not subscriptable
when run speedtest.
this error:
traceback (most recent call last): file "<pyshell#3>", line 1, in <module> speedtest() file "\\internal.sloughgrammar.berks.sch.uk\students$\home\2014\14khandelwala\mydocs\year8\computing\raspberry pi\test_sort.py", line 14, in speedtest insertion(a) file "\\internal.sloughgrammar.berks.sch.uk\students$\home\2014\14khandelwala\mydocs\year8\computing\raspberry pi\test_sort.py", line 41, in insertion val, j = 0[i], i-1 typeerror: 'int' object not subscriptable
this code:
# test_sort.py random import * time import * def speedtest(): print("*** bubble sort vs. insertion sort ***") y in [2000, 4000, 8000]: print("\narray of {} random numbers 0 999:".format(y)) = [randint(1,999) x in range(y)] b = a[:] tic = time() insertion(a) toc = time() print("insertion sort: {} seconds.".format(str(round(toc-tic,1)).rjust(5))) tic = time() bubble(a) toc = time() print("bubble sort: {} seconds.".format(str(round(toc-tic,1)).rjust(5))) def bubble(a): """takes list , sorts it.""" j = len(a) while true: swap = false j -= 1 k in range(j): if a[k] > a[k+1]: a[k], a[k+1] = a[k+1], a[k] swap = true if not swap: return # insertion_sort.py def insertion(a): = [randint(1,999) x in range(y)] """takes list , sorts using insertion sort.""" in range(1, len(a)): val, j = 0[i], i-1 while j >= 0 , a[j] > val: a[j+1] = a[j] j -= 1 a[j+1] = val
val, j = 0[i], i-1
edit line , try instead -
val, j = a[i], i-1
Comments
Post a Comment