""" Write an efficient program to find the hexadecimal equivalent of a given series of octal numbers from m to n.""" import time import sys t0=time.clock() def oc_to_deci(n): r1=repr(n) r2=len(r1) r3=r1[::-1] list=[] list=r3 no=0 r5=list[0] r6=int(r5) r7=list.index(r5) flag=0 for i in range(1,r2): if r6>=8: flag=flag+1 else: r5=list[i] r6=int(r5) if flag==0: for k in range(r2): r5=list[k] r6=int(r5) r7=list.index(r5) for j in range(r7): r6=r6*8 no=no+r6 w=int(no) deci_to_hexa(w) def deci_to_hexa(m): s='' h=m%16 while m>=1: if h!=0: while m%16!=0: t2=m%16 m=m/16 if t2<=9: t3=repr(t2) s=s+t3 else: if t2==10: s=s+'A' elif t2==11: s=s+'B' elif t2==12: s=s+'C' elif t2==13: s=s+'D' elif t2==14: s=s+'E' elif t2==15: s=s+'F' else: print 'none' else: t3=m%16 t4=repr(t3) s=s+t4 m=m/16 p=s[::-1] z.write((p)+",") """Take the input from file and producing the results""" f=open("c:\inputs\in5_6.txt","r") z=open("out_16_sec5_6.txt",'w') str=f.readlines() sum=0 for i in range(len(str)): a=str[i] b=a.split(',') x=int(b[0]) y=int(b[1]) if x>y: diff=x-y else: diff=y-x if x<=y: while x<=y: oc_to_deci(x) x=x+1 else: while y>=x: oc_to_dec1(y) y=y-1 z.write("\n") sum=sum+2*(diff) z.write("additions=") z.write(repr(sum)+"\n") t1=time.clock() t2=t1-t0 mem=sys.getsizeof(a)+sys.getsizeof(b)+264 z.write("time=") z.write(repr(t2)+"\n") z.write('memory uded:=') z.write(repr(mem)) z.close()