I have this psuedocode:
FUNCTION initialize
FOR row = 1 to num_items
get random number for the list
ENDFOR
ENDFUNCTION initializeFUNCTION show
FOR row = 1 to num_items
show output: list(row)
ENDFOR
ENDFUNCTION showFUNCTION sort
FOR pass = 1 to num_items
posn = 1
sorted = True
WHILE posn < num_items
IF list(posn) > list(posn + 1)
swap list(posn) with list(posn + 1)
sorted = False
ENDIF
increment posn
ENDWHILE
IF sorted
Exit the FOR loop
ENDIF
perform show
ENDFOR
ENDFUNCTION sortnum_items = MAX
define a numeric list of size num_itemsperform initialize
perform show
perform sort
That I need to translate into python. so far this is what I have:
from random import randint
list_num=[]
row=0
num=0
num_items=15
def initialize():
for row in range(0,num_items):
number=randint(1,1000)
list_num.append(number)
row +=1
def sorter():
posn=1
for num in range(0,num_items):
srtd=0
while posn<num_items:
if list(posn) >list(posn+1):
a= list_num.index(list_num(posn))
b= list_num.index(list_num(posn+1))
list_num[b],list_num[a]=list_num[a],list_num[b]
srtd=1
posn+=1
else:
posn+=1
if srtd==0:
break
print list_num
initialize()
print list_num
sorter()
But I keep getting this result:
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
>>> [evaluate bubble.py]
[691, 577, 8, 960, 214, 483, 841, 373, 481, 569, 785, 442, 118, 538, 289]
TypeError: iteration over non-sequence
>>>
Any help or advice you have to offer would be a great deal of help.
Thanks,
Jaro