Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Ackermann function expander")
- print("Ackermann function: ")
- print("""ack(m, n):
- ans = 0
- if m == 0:
- ans = n + 1
- elif n == 0:
- ans = ack(m - 1, 1)
- else:
- ans = ack(m - 1, ack(m, n - 1))
- return ans
- """)
- def ack(m,n):
- ans = 0
- if m == 0:
- ans = n + 1
- elif n == 0:
- ans = ack(m - 1, 1)
- else:
- ans = ack(m - 1, ack(m, n - 1))
- return ans
- # This isn't actually used, all the ackering is done in the one below
- def ackEx(ackFunctionString):
- inp = ackFunctionString[4:-1]
- inp = inp.split(",")
- m = int(inp[0])
- n = int(inp[1])
- ans = ""
- if m == 0:
- ans = str(n + 1)
- elif n == 0:
- ans = "ack(" + str(m - 1) + ", " + "1)"
- else:
- ans = "ack(" + str(m - 1) + ", ack(" + str(m) + ", " + str(n -1) + "))"
- return ans
- def FindLowestFunction(s, func):
- #(String with expression, function prefix (ie, in sin(2 + sin(3 - sin(4 + x))), use FLF(..., sin)
- Open = "("
- Close = ")"
- startIndex = 0
- endIndex = 0
- index = 0
- for char in s:
- if char == Open:
- startIndex = index
- elif char == Close:
- endIndex = index
- break
- index += 1
- startIndex -= len(func)
- res = [s[:startIndex], s[startIndex:endIndex + 1], s[endIndex + 1:]]
- return res
- inp = input("Type 2 integers, seperated by a comma: ")
- print("Press enter to expand the next line.")
- func = ""
- while inp:
- try:
- inp = inp.split(",")
- m = int(inp[0])
- n = int(inp[1])
- done = False
- func = "ack({0}, {1})".format(m, n)
- print(" = " + func)
- func = ackEx(func)
- while not done:
- func = FindLowestFunction(func, "ack")
- func = func[0] + ackEx(func[1]) + func[2]
- print(" = " + func, end = "")
- if func[0] != "a":
- done = True
- print("\nDone!")
- input()
- except ValueError:
- print("There was a ValueError. Make sure the input is formatted correctly.")
- except IndexError:
- print("There was an IndexError. Make sure you type in two numbers.")
- inp = input("Type 2 integers, seperated by a comma:")
- print("Press enter to expand the next line.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement