import time import sys class Stack : def __init__(self) : self.items = [] def push(self, item) : self.items.append(item) def pop(self) : return self.items.pop() def isEmpty(self) : return (self.items == []) f=open('in8_5.txt','r') s=f.readline() i=s.index(",") n=int(s[0:i]) m=int(s[i+1:len(s)]) f.readline() a=f.readline() sh=a.split(",") f.readline() b=f.readline() sh1=b.split(",") f.close() for i in range(0,len(sh)): sh[i]=int(sh[i]) for i in range(0,len(sh1)): sh1[i]=int(sh1[i]) f1=open('out_15_8_5.txt','w') s=Stack() s2=Stack() no_psh=0 no_pop=0 sh1.sort() t1=time.clock() for i in range (0,len(sh)): no_psh=no_psh+1 s.push(sh[i]) for i in range (0,m): count=n-1 while(count>=sh1[i]): s2.push(s.pop()) no_psh=no_psh+1 no_pop=no_pop+1 count=count-1 s2.pop() s.push(sh1[i]) while not s2.isEmpty(): s.push(s2.pop()) no_pop=no_pop+1 f1.write("after insertion:") while not s.isEmpty(): f1.write("\n") f1.write(str(s.pop())) for i in range (0,len(sh)): s.push(sh[i]) for i in range (0,m): count=n-1 while(count>=sh1[i]): s2.push(s.pop()) no_psh=no_psh+1 no_pop=no_pop+1 count=count-1 s2.pop() while not s2.isEmpty(): s.push(s2.pop()) no_pop=no_pop+1 f1.write("\nafter deletion:") no_ele=0 while not s.isEmpty(): f1.write("\n") no_ele=no_ele+1 f1.write(str(s.pop())) t2=time.clock() t=t2-t1 mem=sys.getsizeof(s)+sys.getsizeof(sh)+sys.getsizeof(sh1)+sys.getsizeof(s2)+sys.getsizeof(no_psh)+sys.getsizeof(no_pop) f1.write("\nno.of elements left in stack=%d\npushes=%d\npops=%d\ntime taken=%f\nmemory used=%dbytes"%(no_ele,no_psh,no_pop,t,mem)) f1.close()