Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import math
- import sys
- random = [1562469902, 1039845534, 2025653534, 739593874, 994290584, 1198075102, 605335584, 563009619, 1076425455, 1979353639, 1481705266, 416282717, 1502074844, 339011283, 1656724019, 75412011, 296441807, 1150973001, 1935872936, 378814183, 1318686473, 2034028701, 1310947874, 2095686658, 1548890542, 987301502, 543737933, 1987654433] #hard coded r up to 50 numbers
- templist = ['']
- operand = ["*", "+", "-", "%", "/", "d", "r", "=", "^"] #these are operators (i know it says operands but ive used the variable to many times to change its name now)
- n = 1 #variable to keep the program in one loop
- e = 0
- def operator():
- def times(x, y):
- return x * y #functions with operation functions
- def add(x, y):
- return x + y
- def minus(x, y):
- return y - x
- def perc(x, y):
- return abs(y) % abs(x)
- def divide(x, y):
- return y / x
- def power(x, y):
- return y**x
- return (lambda z : {'^':power, '/':divide, '*':times, '+':add, '-':minus, '%':perc}[z])
- def octal(x):
- return int(x, 8)
- op = operator()
- overflow = False #over flow variable
- try:
- while n > 0:
- x = raw_input()
- x = str(x).split('#')[0] # removes anything after the '#'
- x = str(x).split(" ") # splits main input"44 443" into ["43","443"]
- for i in x:
- if len(i) == 0:
- pass
- elif i not in operand and overflow == True: # Uses the variable Overflow if true nothing but operand and process them until stack isnt full
- sys.stderr.write("Stack overflow."'\n')
- continue
- elif i in operand and overflow == True and i != 'r':
- templist.append(i)
- break
- try:
- if i in operand:
- templist.append(str(i)) # all individual values input for operators are appended to templist
- elif len(i) == 0: # all gaps are ignored (empty strings)
- pass
- elif type(int(i)) == type(1) or type(long(i)) == type(5196768676867867867868867): #accepts integer and long octals and appends them to templist
- s = i
- q = list(s)
- p = len(q)-1
- if q[0] in operand and q[0] != "-":
- int(pi)
- try:
- while p > -1:
- if (q[p] == '8' or q[p] == '9') and q[0] == '0':
- s = str(s).split('8')[0]
- s = str(s).split('9')[0]
- q = list(s) # now this chunk of code will check through all the single items and see if they are integers
- break #it will also check if they are octals which truncate the octal if 9 or 8 in included
- elif (q[p] == '8' or q[p] == '9') and q[0]=='-':
- s = str(s).split('8')[0]
- s = str(s).split('9')[0]
- q = list(s)
- break
- elif q[0] == '-' and q[1] != '0':
- int(pi)
- elif q[0] != '0' and q[0] != "-":
- int(pi)
- else:
- p = p - 1
- templist.append(octal(s)) #single octals appended her
- except:
- templist.append(int(s)) #single integers appended here
- except (ValueError, NameError): #IF SINGLE NOT FLOAT/INTEGER PASS (error letters) a except will pick up everything not appended
- y = list(i)
- z=0
- for j in y:
- if j in operand and y[0] != "-":
- y.remove(j)
- y.insert(z," "+j+" ") #this separates the combined value by looking for operators and replacing them with a space either side
- elif j == "-" and y[0] == "-":
- y[0]=" -"
- z=z+1
- y = "".join(y)
- y = y.split(" ")
- y = filter (None, y) #filters out empty strings and joins the list
- f= y
- for k in f: # once joint the same procress happens again checking for integers/octals /operators
- s = k
- q = list(s)
- if k in operand != "=":
- templist.append(str(k))
- else:
- try:
- if type(int(k)) == type(1):
- p = len(k)-1
- try:
- while p > -1:
- if (q[p] == '8' or q[p] == '9') and q[0] == '0':
- s = str(s).split('8')[0]
- s = str(s).split('9')[0]
- q = list(s)
- break
- elif (q[p] == '8' or q[p] == '9') and q[0] == '-':
- s = str(s).split('8')[0]
- s = str(s).split('9')[0]
- q = list(s)
- break
- elif q[0] == '-' and q[1] != '0':
- int(pi)
- elif q[0] != '0' and q[0] != '-':
- int(pi)
- else:
- p = p - 1
- templist.append(octal(s)) #multiple octals appended here
- except:
- templist.append(s) #multiple integers appended here
- except (ValueError):
- v = q
- z = 0
- for element in v:
- try:
- if type(int(element)) == type(1):
- z = z + 1
- except:
- sys.stderr.write(('Unrecognised operator or operand "')+str(element)+('".')+('\n')) #Error for operators apart from ones in list
- v[z] = " "
- z = z + 1
- v = "".join(v) #if numeric values are entered within unrecognised operators eg 'e3232'
- v = v.split(" ") #this will do that and append the final octals and integers
- for i in v:
- try:
- octal(i)
- templist.append(octal(i)) #octals appended here
- except (ValueError):
- templist.append(i) #integers appended here
- for i in templist:
- a = templist.index(i)
- if i in operand:
- if i != '=' and i !='d' and i != 'r' and i != "%": # this is for all of the operators apart from ones mentioned
- n = 1
- m = 1
- while True: #counters m and n go through the list to see if the valuess before the operators are integers
- try:
- if templist[a - n] == "" or templist[a - n] in operand:
- n = n + 1
- if a-n<0:
- int(pi)
- elif templist[a - n - m] == "" or templist[a - n - m] in operand:
- m = m + 1
- if a-n-m<0: #int(pi) used as a dummy error to stop the operation since there is an underflow
- int(pi)
- elif (a-n-m)>0 and (a-n)>0:
- try:
- if (int(templist[a-n-m])>=0 and int(templist[a-n])>=0) or (int(templist[a-n-m])<=0 and int(templist[a-n])<=0):
- templist[a] = (op(i)(float(templist[a - n]), int(templist[ a - n - m])))
- templist[a] = int(templist[a])
- if templist[a] >2**31-1:
- templist[a]= 2**31-1
- elif templist[a] < -(2**31): #once m and n are fixed this block of code handles saturation and calculates
- templist[a]= -(2**31)
- templist[a - n] = ""
- templist[a - n - m] = ""
- break
- elif (int(templist[a-n-m])<=0 and int(templist[a-n])>=0) or (int(templist[a-n-m])>=0 and int(templist[a-n])<=0):
- templist[a] = (op(i)(float(templist[a - n]), int(templist[ a - n - m])))
- templist[a] = int(templist[a])
- templist[a] = templist[a]
- if templist[a] <-(2**31):
- templist[a]= -(2**31)
- elif templist[a] >(2**31)-1:
- templist[a]= (2**31)-1
- templist[a - n] = ""
- templist[a - n - m] = ""
- break
- except (ZeroDivisionError):
- sys.stderr.write("Divide by 0."'\n') #zero division errors caught here
- templist[templist.index("/")] = ""
- break
- else:
- int(pi) #different cases of index errors considered also dummy name errors caught here
- except (IndexError, NameError):
- sys.stderr.write("Stack underflow."'\n')
- templist[a] = ""
- break
- elif i == '%':
- n = 1
- m = 1
- while True: #very similar block of text for modulo, sign errors are fixed here rounding errors are fixed here and also division errors are caught out
- try: #and it also calculates teh modulo
- if templist[a - n] == "" or templist[a - n] in operand:
- n = n + 1
- if a-n<0:
- int(pi)
- elif templist[a - n - m] == "" or templist[a - n - m] in operand:
- m = m + 1
- if a-n-m<0:
- int(pi)
- else:
- try:
- if (int(templist[a-n-m])>=0 and int(templist[a-n])>=0) or (int(templist[a-n-m])<=0 and int(templist[a-n])<=0):
- templist[a] = (op(i)(int(templist[a - n]), int(templist[ a - n - m])))
- if templist[a] >2**31-1:
- templist[a]= 2**31-1
- else:
- pass
- templist[a - n] = ""
- templist[a - n - m] = ""
- break
- elif (int(templist[a-n-m])<=0 and int(templist[a-n])>=0) or (int(templist[a-n-m])>=0 and int(templist[a-n])<=0):
- templist[a] = (op(i)(int(templist[a - n]), int(templist[ a - n - m])))
- if (int(templist[a-n-m])<0) and int((templist[a-n])>0):
- templist[a] = -(templist[a])
- elif (int(templist[a-n-m]))>0 and int((templist[a-n])<0):
- templist[a] = templist[a]
- if templist[a] <-(2**31):
- templist[a]= -(2**31)
- else:
- pass
- templist[a - n] = ""
- templist[a - n - m] = ""
- break
- except (ZeroDivisionError):
- sys.stderr.write("Divide by 0."'\n')
- templist[templist.index("%")] = ""
- break
- except (IndexError, NameError):#type/value/tikname/tikindex
- sys.stderr.write("Stack underflow."'\n')
- templist[a] = ""
- break
- elif i == '=':
- n=1
- try:
- if templist[a-n]== "" or templist[a-n] in operand:
- while templist[a-n] == "" or templist[a-n] in operand:
- n = n + 1 #the equals operand defined here, it looks though the list for the first value before it and prints to output
- templist[a] = ""
- print templist[a-n]
- elif a-1<0:
- int(pi)
- else:
- print templist[ a - 1]
- templist[a] = ""
- except (NameError, IndexError):
- sys.stderr.write("Stack empty."'\n') #empty stacks caught here with the use of index and dummy errors
- templist[a] = ""
- break
- elif i == 'd':
- b = 0
- while b < a:
- if templist[b] == "":
- b = b + 1 #d does a very similar job but prints out every value before d
- else:
- print templist[b]
- b = b + 1
- templist[a] = ""
- elif i == 'r':
- try:
- templist[a] = random[e]
- e = e + 1
- except IndexError:
- e = 0
- blank = 0
- flow = 0
- for i in templist:
- if i == "": #this block checks for stack overflow by checking how many objects that have values are in list and stops all appends but operators
- blank = blank + 1 # untill
- else:
- flow = flow + 1
- if flow >22:
- overflow = True
- else:
- overflow = False
- except KeyboardInterrupt:
- print ""
- sys.exit()
- except (EOFError): #these catch any end of file errors and exit cleanly
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement