defempty(q):return len(q)==0defget_from_print(code):
cmd = code.split(' ')
return Value[cmd[1]]
defget_var(code):
cmd = code.split(' ')
return cmd[0],cmd[2]
defType(code):if code=='lock':return0elif code=='unlock':return1elif code[0:3]=='end':return2elif code[0:5]=='print':return3else:return4defprocess(n,t,Q):
print(t)
programs_left = n
index = 1while programs_left != 0:
line = input()
if line[0:3]=='end':p[index].append(line);index+=1;p[index] = [];programs_left-=1;continue
p[index].append(line)#put the input into the p[index]for i in range(n):waitque.append(i+1)
islocked = Falsewhilenot empty(waitque):
program = waitque.popleft()
remainder = Q
blocked = Falsewhile remainder > 0:
code = p[program][0]
code_type = Type(code)
#print('case:',program,'codetype******',code_type,code)if code_type == 0:#.....................lockif remainder>= t[code_type]:remainder -= t[code_type]
else:break# no enough time to run the cmdifnot islocked:
islocked = Trueelse:
blocked = True
blockque.append(program)#put program into the blockquebreak#.........................would not delete the code beacuse it failedelif code_type == 1 :#..................unlockif remainder>= t[code_type]:remainder -= t[code_type]
else:break# no enough time to run the cmd
islocked = Falseifnot empty(blockque):
another_program = blockque.popleft()
waitque.appendleft(another_program)#put another_program into the first of waitqueelif code_type == 2:#...................endif remainder>= t[code_type]:remainder -= t[code_type]
else:break# no enough time to run the cmd
remainder = 0elif code_type == 3:#.......................printif remainder>= t[code_type]:remainder -= t[code_type]
else:break# no enough time to run the cmd
var = get_from_print(code)
print(program,':',var)#output program,the value of varelse :#.....................................assignmentif remainder>= t[code_type]:remainder -= t[code_type]
else:break# no enough time to run the cmd
var,constant = get_var(code)
Value[var] = constant
p[program].remove(code)#delte the code which has been runifnot empty(p[program]) andnot blocked:
waitque.append(program)#put program into the end of waitquedefget_args():
line = input()
line = line.split(' ')
n,t1,t2,t3,t4,t5,Q = line
n,t,Q = int(n),[int(t1),int(t2),int(t3),int(t4),int(t5)],int(Q)
return [n,t,Q]
process(*get_args())
deftest(enter,depart):
i,k,n = 1,0,len(depart)
new = []
while k<n:
new_items = 1
new.append(depart[k])
while i<n:
if i==depart[k]:i+=1;break
new.append(i)#push i into newif new[i]!=depart[i]:returnFalse
i+=1
new_items += 1
k+= new_items
returnTrue
print(test([1,2,3,4,5],[5,4,1,2,3]))
例题6−3:利用栈来求表达式,矩阵链乘
defmatrix(expression):deftimes(a,b):return a[0]*b[0]*b[1]
defmul(a,b):return (a[0],b[1])
defok(a,b):return a[1]==b[0]
sum,s=0,[]
for each in expression:
if each.isalpha():s.append(record[each]);print(each,s)
elif each==')':
b = s.pop()
a = s.pop()
if ok(a,b):s.append(mul(a,b));sum+=times(a,b);print(s,times(a,b))
else:return'error'if len(s)!=1:
sum += times(s[1],s[0])
return sum
record = {}
defprocess():
n = int(input())
while n>0:
line = input()
line = line.split(' ')
record[line[0]] = (int(line[1]),int(line[2]))
n-=1while1:
exp = input()
if exp=='':break
print(matrix(exp))
process()
defproc2():
line = ' '+input()
while line!='':
curr,last=0,0
next = [0]*(len(line)+5)
for i,ch in enumerate(line):
if ch=='[':curr=0elif ch==']':curr=last
else:#simulate the insert operation
next[i] = next[curr]
next[curr] = i
if curr==last:last=i#update the last
curr = i
i = next[0]
print(next)
while i!=0:
print(line[i],end='')
i = next[i]
print()
line = input()
proc2()
defGet(cmd):
cmd = cmd.split(' ')
if len(cmd)==3:
return int(cmd[0]),int(cmd[1]),int(cmd[2])
else:
return4,None,Nonedefproc(n,cmds):
next =[i+1for i in range(n+5)]
pre = [i-1for i in range(n+5)]
rev = 0deflink(L,R):
next[L],pre[R] = R,L
for cmd in cmds:
Type,x,y = Get(cmd)
if rev==1and Type<2:Type = 3-Type
if Type==3and next[y]==x:x,y = y,x
if x and y:p_x,p_y,n_x,n_y = pre[x],pre[y],next[x],next[y]
if Type == 2:#rightifnot next[y]==x:#x is not in the right of y:,
link(p_x,n_x);link(y,x);link(x,n_y)
elif Type == 1:#leftifnot next[x]==y:
link(p_y,x);link(x,y);link(p_x,n_x)
elif Type == 3:
ifnot next[x]==y:
link(p_x,y);link(y,n_x);link(p_y,x);link(x,n_y)
else:
link(p_x,y);link(y,x);link(x,n_y)
else:#reverse all the list
rev = 1
sum,i,first = 0,1,next[0]
while i<=n:
if (n-i+1if rev else i)%2==1:sum+=first
first,i= next[first],i+1
print(sum)
deffunc():
line = input()
while line!='':
cmds = []
line = line.split(' ')
n,num = int(line[0]),int(line[1])
line
while num:
cmd = input()
cmds.append(cmd)
num-=1
proc(n,cmds)
line = input()
func()