Advertisement
Leeeo

Ackermann Expander

Jul 17th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. print("Ackermann function expander")
  2. print("Ackermann function: ")
  3. print("""ack(m, n):
  4.    ans = 0
  5.    if m == 0:
  6.        ans = n + 1
  7.    elif n == 0:
  8.        ans = ack(m - 1, 1)
  9.    else:
  10.        ans = ack(m - 1, ack(m, n - 1))
  11.    return ans
  12. """)
  13. def ack(m,n):
  14.     ans = 0
  15.     if m == 0:
  16.         ans = n + 1
  17.     elif n == 0:
  18.         ans = ack(m - 1, 1)
  19.     else:
  20.         ans = ack(m - 1, ack(m, n - 1))
  21.     return ans
  22.     # This isn't actually used, all the ackering is done in the one below
  23. def ackEx(ackFunctionString):
  24.     inp = ackFunctionString[4:-1]
  25.     inp = inp.split(",")
  26.     m = int(inp[0])
  27.     n = int(inp[1])
  28.     ans = ""
  29.     if m == 0:
  30.         ans = str(n + 1)
  31.     elif n == 0:
  32.         ans = "ack(" + str(m - 1) + ", " + "1)"
  33.     else:
  34.         ans = "ack(" + str(m - 1) + ", ack(" + str(m) + ", " + str(n -1) + "))"
  35.     return ans
  36.  
  37. def FindLowestFunction(s, func):
  38.     #(String with expression, function prefix (ie, in sin(2 + sin(3 - sin(4 + x))), use FLF(..., sin)
  39.     Open = "("
  40.     Close = ")"
  41.     startIndex = 0
  42.     endIndex = 0
  43.     index = 0
  44.     for char in s:
  45.         if char == Open:
  46.             startIndex = index
  47.         elif char == Close:
  48.             endIndex = index
  49.             break
  50.         index += 1
  51.     startIndex -= len(func)
  52.     res = [s[:startIndex], s[startIndex:endIndex + 1], s[endIndex + 1:]]
  53.     return res
  54.  
  55.  
  56. inp = input("Type 2 integers, seperated by a comma: ")
  57. print("Press enter to expand the next line.")
  58. func = ""
  59. while inp:
  60.     try:
  61.         inp = inp.split(",")
  62.         m = int(inp[0])
  63.         n = int(inp[1])
  64.         done = False
  65.         func = "ack({0}, {1})".format(m, n)
  66.         print(" = " + func)
  67.         func = ackEx(func)
  68.         while not done:
  69.             func = FindLowestFunction(func, "ack")
  70.             func = func[0] + ackEx(func[1]) + func[2]
  71.             print(" = " + func, end = "")
  72.             if func[0] != "a":
  73.                 done = True
  74.                 print("\nDone!")
  75.             input()
  76.     except ValueError:
  77.         print("There was a ValueError. Make sure the input is formatted correctly.")
  78.     except IndexError:
  79.         print("There was an IndexError. Make sure you type in two numbers.")
  80.     inp = input("Type 2 integers, seperated by a comma:")
  81.     print("Press enter to expand the next line.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement