return infinite loop python success -
i learning python online not understand last step should take.
here question
write function execute(prog) following. assume prog list of strings containing basic program, before. then, function should return either "success" or "infinite loop"
depending on whether program terminates, or loops forever. important: should assume procedure findline(prog, target)
defined in sub-task 2 defined, not need re-write it. hint
input example:(['5 goto 30', '10 goto 20', '20 goto 10', '30 goto 40', '40 end'])
my code still returns "infinite loop"
when should return success cannot find last required step. ( include debugging checks here also)
def execute(prog): location = 0 #print('prog = ', prog) visited = [false] * len(prog) while true: in range (0,len(prog)): visited[i] = true #print('prog type=',type(prog)) # print(type(location)) #print('len of prog=', len(prog)) #for in range (0,len(prog)): # visited[i] = true # print('visited =', visited) # print('visited[i] =', visited[i]) # print('prog[i] =', prog[i]) findt = prog[location].split() #print('findt = ',findt) t = findt[-1] #print('t=',t) location = findline(prog, t) #print('new location=', location) if visited[location]: return "infinite loop" elif location==len(prog)-1: return "success" #print('t=',t) #print('prog = ', prog)
hint: here getbasic() , findline() code wrote , works:
def getbasic(): mylist = [] while true: line = str(input()) mylist.append(line) if line.endswith("end"): break return mylist def findline(prog, target): in range(0, len(prog)): #range doesn't include last element. progx = prog[i].split() if progx[0] == target: return
this correct version of code :
def execute(prog): location = 0 counter = 0 while true: findt = prog[location].split() t = findt[-1] location = findline(prog , t) counter += 1 if t == 'end': return "success" if counter > len(prog): return "infinite loop"
in case use short way , easy way solution :)
Comments
Post a Comment