Advertisement
Guest User

My First Python Code

a guest
Feb 9th, 2021
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.79 KB | None | 0 0
  1. #!/usr/bin/python
  2. import math
  3. import sys
  4. 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
  5. templist = ['']
  6. operand = ["*", "+", "-", "%", "/", "d", "r", "=", "^"]    #these are operators (i know it says operands but ive used the variable to many times to change its name now)
  7. n = 1                                              #variable to keep the program in one loop
  8. e = 0
  9. def operator():
  10.     def times(x, y):
  11.         return x * y                             #functions with operation functions
  12.     def add(x, y):
  13.         return x + y
  14.     def minus(x, y):
  15.         return y - x
  16.     def perc(x, y):
  17.         return abs(y) % abs(x)
  18.     def divide(x, y):
  19.         return y / x
  20.     def power(x, y):
  21.         return y**x
  22.     return (lambda z : {'^':power, '/':divide, '*':times, '+':add, '-':minus, '%':perc}[z])
  23. def octal(x):
  24.     return int(x, 8)
  25. op = operator()
  26.  
  27. overflow = False             #over flow variable
  28.  
  29. try:
  30.     while n > 0:
  31.         x = raw_input()
  32.         x = str(x).split('#')[0]            # removes anything after the '#'
  33.         x = str(x).split(" ")               # splits main input"44 443" into ["43","443"]
  34.         for i in x:
  35.             if len(i) == 0:
  36.                 pass
  37.             elif i not in operand and overflow == True:     # Uses the variable Overflow if true nothing but operand and process them until stack isnt full
  38.                 sys.stderr.write("Stack overflow."'\n')
  39.                 continue
  40.             elif i in operand and overflow == True and i != 'r':
  41.                 templist.append(i)
  42.                 break
  43.             try:
  44.                 if i in operand:
  45.                     templist.append(str(i))        # all individual values input for operators are appended to templist
  46.                 elif len(i) == 0:                  # all gaps are ignored (empty strings)  
  47.                     pass            
  48.                 elif type(int(i)) == type(1) or type(long(i)) == type(5196768676867867867868867):         #accepts integer and long octals and appends them to templist
  49.                     s = i
  50.                     q = list(s)
  51.                     p = len(q)-1
  52.  
  53.                     if q[0] in operand and q[0] != "-":
  54.                         int(pi)
  55.                     try:
  56.                         while p > -1:
  57.                             if (q[p] == '8' or q[p] == '9') and q[0] == '0':
  58.                                 s = str(s).split('8')[0]
  59.                                 s = str(s).split('9')[0]
  60.                                 q = list(s)                                           # now this chunk of code will check through all the single items and see if they are integers
  61.                                 break                                                  #it will also check if they are octals which truncate the octal if 9 or 8 in included
  62.                             elif (q[p] == '8' or q[p] == '9') and q[0]=='-':  
  63.                                 s = str(s).split('8')[0]
  64.                                 s = str(s).split('9')[0]
  65.                                 q = list(s)
  66.                                 break
  67.                             elif q[0] == '-' and q[1] != '0':
  68.                                 int(pi)
  69.                             elif q[0] != '0' and q[0] != "-":
  70.                                 int(pi)                                    
  71.                             else:
  72.                                 p = p - 1
  73.                         templist.append(octal(s))  #single octals appended her
  74.                     except:
  75.                         templist.append(int(s))         #single integers appended here
  76.                        
  77.                                      
  78.             except (ValueError, NameError):        #IF SINGLE NOT FLOAT/INTEGER PASS (error letters) a except will pick up everything not appended
  79.                 y = list(i)                                
  80.                 z=0
  81.                 for j in y:
  82.                     if j in operand and y[0] != "-":
  83.                         y.remove(j)
  84.                         y.insert(z," "+j+" ")             #this separates the combined value by looking for operators and replacing them with a space either side
  85.  
  86.                     elif j == "-" and y[0] == "-":
  87.                         y[0]=" -"
  88.                     z=z+1
  89.  
  90.                 y = "".join(y)
  91.                 y = y.split(" ")      
  92.                 y = filter (None, y)          #filters out empty strings and joins the list
  93.                
  94.                 f= y
  95.                 for k in f:                        # once joint the same procress happens again checking for integers/octals /operators
  96.                     s = k
  97.                     q = list(s)                                  
  98.                     if k in operand != "=":
  99.                         templist.append(str(k))
  100.                     else:
  101.                         try:
  102.                             if type(int(k)) == type(1):      
  103.                                 p = len(k)-1
  104.                                 try:
  105.                                     while p > -1:    
  106.                                         if (q[p] == '8' or q[p] == '9') and q[0] == '0':
  107.                                             s = str(s).split('8')[0]
  108.                                             s = str(s).split('9')[0]
  109.                                             q = list(s)
  110.                                             break
  111.                                         elif (q[p] == '8' or q[p] == '9') and q[0] == '-':
  112.                                             s = str(s).split('8')[0]
  113.                                             s = str(s).split('9')[0]
  114.                                             q = list(s)
  115.                                             break
  116.                                         elif q[0] == '-' and q[1] != '0':
  117.                                             int(pi)
  118.                                         elif q[0] != '0' and q[0] != '-':
  119.                                             int(pi)
  120.                                         else:
  121.                                             p = p - 1
  122.                                     templist.append(octal(s))  #multiple octals appended here
  123.                                 except:
  124.                                     templist.append(s)         #multiple integers appended here
  125.                         except (ValueError):
  126.                             v = q
  127.                             z = 0
  128.                             for element in v:
  129.                                 try:                                          
  130.                                     if type(int(element)) == type(1):              
  131.                                         z = z + 1
  132.                                 except:
  133.                                     sys.stderr.write(('Unrecognised operator or operand "')+str(element)+('".')+('\n'))     #Error for operators apart from ones in list
  134.                                     v[z]  = " "
  135.                                     z = z + 1
  136.                             v = "".join(v)                               #if numeric values are entered within unrecognised operators eg 'e3232'
  137.                             v = v.split(" ")                            #this will do that and append the final octals and integers
  138.                             for i in v:
  139.                                 try:
  140.                                     octal(i)
  141.                                     templist.append(octal(i))  #octals appended here
  142.                                 except (ValueError):
  143.                                     templist.append(i)        #integers appended here
  144.                                
  145.  
  146.  
  147.         for i in templist:
  148.             a = templist.index(i)
  149.             if i in operand:
  150.                 if i != '=' and i !='d' and i != 'r' and i != "%":   # this is for all of the operators apart from ones mentioned
  151.                     n = 1
  152.                     m = 1
  153.                     while True:              #counters m and n go through the list to see if the valuess before the operators are integers
  154.                         try:
  155.                             if templist[a - n] == "" or templist[a - n] in operand:
  156.                                 n = n + 1
  157.                                 if a-n<0:
  158.                                     int(pi)
  159.                             elif templist[a - n - m] == "" or templist[a - n - m] in operand:
  160.                                 m = m + 1
  161.                                 if a-n-m<0:          #int(pi) used as a dummy error to stop the operation since there is an underflow
  162.                                     int(pi)
  163.                             elif (a-n-m)>0 and (a-n)>0:
  164.                                 try:
  165.                                     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):                      
  166.                                         templist[a] = (op(i)(float(templist[a - n]), int(templist[ a - n - m])))
  167.                                         templist[a] = int(templist[a])
  168.                                         if templist[a] >2**31-1:
  169.                                             templist[a]= 2**31-1
  170.                                         elif templist[a] < -(2**31):                    #once m and n are fixed this block of code handles saturation and calculates
  171.                                             templist[a]= -(2**31)                      
  172.                                         templist[a - n] = ""
  173.                                         templist[a - n - m] = ""
  174.                                         break
  175.                                     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):                          
  176.                                         templist[a] = (op(i)(float(templist[a - n]), int(templist[ a - n - m])))
  177.                                         templist[a] = int(templist[a])
  178.                                         templist[a] = templist[a]
  179.                                         if templist[a] <-(2**31):
  180.                                             templist[a]= -(2**31)
  181.                                         elif templist[a] >(2**31)-1:
  182.                                             templist[a]= (2**31)-1
  183.                                         templist[a - n] = ""
  184.                                         templist[a - n - m] = ""
  185.                                         break
  186.  
  187.  
  188.                                 except (ZeroDivisionError):
  189.                                     sys.stderr.write("Divide by 0."'\n')           #zero division errors caught here
  190.                                     templist[templist.index("/")] = ""
  191.                                     break
  192.                             else:
  193.                                 int(pi)                              #different cases of index errors considered also dummy name errors caught here
  194.                        
  195.                         except (IndexError, NameError):
  196.                             sys.stderr.write("Stack underflow."'\n')
  197.                             templist[a] = ""
  198.                             break  
  199.  
  200.                
  201.                 elif i == '%':  
  202.                     n = 1
  203.                     m = 1
  204.                     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
  205.                         try:          #and it also calculates teh modulo
  206.                             if templist[a - n] == "" or templist[a - n] in operand:
  207.                                 n = n + 1
  208.                                 if a-n<0:
  209.                                     int(pi)
  210.                             elif templist[a - n - m] == "" or templist[a - n - m] in operand:
  211.                                 m = m + 1
  212.                                 if a-n-m<0:
  213.                                     int(pi)
  214.                             else:
  215.                                 try:
  216.                                     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):                          
  217.                                         templist[a] = (op(i)(int(templist[a - n]), int(templist[ a - n - m])))
  218.                                         if templist[a] >2**31-1:
  219.                                             templist[a]= 2**31-1
  220.                                         else:
  221.                                             pass
  222.                                         templist[a - n] = ""
  223.                                         templist[a - n - m] = ""
  224.                                         break
  225.                                     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):                          
  226.                                         templist[a] = (op(i)(int(templist[a - n]), int(templist[ a - n - m])))                                      
  227.                                         if (int(templist[a-n-m])<0) and int((templist[a-n])>0):
  228.                                             templist[a] = -(templist[a])
  229.                                         elif (int(templist[a-n-m]))>0 and int((templist[a-n])<0):
  230.                                             templist[a] = templist[a]
  231.  
  232.                                         if templist[a] <-(2**31):
  233.                                             templist[a]= -(2**31)
  234.                                         else:
  235.                                             pass
  236.                                         templist[a - n] = ""
  237.                                         templist[a - n - m] = ""
  238.                                         break
  239.                                        
  240.  
  241.                                 except (ZeroDivisionError):
  242.                                     sys.stderr.write("Divide by 0."'\n')
  243.                                     templist[templist.index("%")] = ""
  244.                                     break
  245.                         except (IndexError, NameError):#type/value/tikname/tikindex
  246.                             sys.stderr.write("Stack underflow."'\n')
  247.                             templist[a] = ""
  248.                             break                                
  249.  
  250.                 elif i == '=':
  251.                     n=1
  252.                     try:
  253.                         if templist[a-n]== "" or templist[a-n] in operand:
  254.                             while templist[a-n] == "" or templist[a-n] in operand:
  255.                                 n = n + 1                         #the equals operand defined here, it looks though the list for the first value before it and prints to output
  256.                             templist[a] = ""
  257.                             print templist[a-n]
  258.                         elif a-1<0:
  259.                             int(pi)
  260.                         else:
  261.                             print templist[ a - 1]
  262.                             templist[a] = ""
  263.                     except (NameError, IndexError):
  264.                         sys.stderr.write("Stack empty."'\n')        #empty stacks caught here with the use of index and dummy errors
  265.                         templist[a] = ""
  266.                         break
  267.                    
  268.                 elif i == 'd':
  269.                     b = 0
  270.                     while b < a:
  271.                         if templist[b] == "":
  272.                             b = b + 1                             #d does a very similar job but prints out every value before d
  273.                         else:
  274.                             print templist[b]
  275.                             b = b + 1
  276.                     templist[a] = ""
  277.                 elif i == 'r':
  278.                     try:
  279.                         templist[a] = random[e]
  280.                         e = e + 1
  281.                     except IndexError:
  282.                         e = 0
  283.         blank = 0
  284.         flow = 0
  285.         for i in templist:
  286.             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
  287.                 blank = blank + 1                # untill
  288.             else:
  289.                 flow = flow + 1
  290.         if flow >22:
  291.             overflow = True
  292.         else:
  293.             overflow = False
  294.        
  295. except KeyboardInterrupt:
  296.     print ""
  297.     sys.exit()
  298. except (EOFError):                      #these catch any end of file errors and exit cleanly
  299.     pass        
  300.    
  301.  
  302.    
  303.  
  304.  
  305.  
  306.  
  307.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement