Skip to content

Instantly share code, notes, and snippets.

@lool
Created September 10, 2024 19:40
Show Gist options
  • Save lool/352c2e58355cd475e58c269ade533929 to your computer and use it in GitHub Desktop.
Save lool/352c2e58355cd475e58c269ade533929 to your computer and use it in GitHub Desktop.
GEB – The MU Puzzle
import unittest
def rule1(string):
"""Adds "U" to the end of a string if it ends with "I".
Args:
string: The input string.
Returns:
The modified string with "U" added if applicable.
"""
if string.endswith("I"):
#print("Returning %s" % string + "U")
yield string + "U"
def rule2(string):
"""Returns "Mxx" for theorems in "Mx".
Args:
string: The input string.
Returns:
The modified string "Mxx" if applicable.
"""
if string.startswith("M"):
yield "M" + string[1:] + string[1:]
def rule3(string):
"""Replaces "III" with "U".
Args:
string: The input string.
Returns:
The modified strings if applicable.
"""
index = 0
while True:
index = string.find("III", index)
if index == -1:
break
yield string[0:index] + "U" + string[index+len("III"):]
index += 1
def rule4(string):
"""Replaces "UU" with "".
Args:
string: The input string.
Returns:
The modified strings if applicable.
"""
index = 0
while True:
index = string.find("UU", index)
if index == -1:
break
yield string[0:index] + string[index+len("UU"):]
index += 1
def gen_rules():
axioms = set(("MI",))
theorems = axioms
found_new_theorems = True
depth = 0
while found_new_theorems and depth < 6:
print("Depth = %s" % depth)
depth += 1
found_new_theorems = False
for theorem in theorems.copy():
for rule in (rule1, rule2, rule3, ):
for string in rule(theorem):
#print("Looking at %s" % string)
if string not in theorems:
theorems.add(string)
found_new_theorems = True
print("Found %s" % string)
class TestAddU(unittest.TestCase):
def test_ends_with_i(self):
result = rule1("MI")
self.assertEqual(next(result), "MIU")
def test_does_not_end_with_i(self):
result = rule1("MU")
with self.assertRaises(StopIteration):
next(result)
def test_empty_string(self):
result = rule1("")
with self.assertRaises(StopIteration):
next(result)
if __name__ == '__main__':
gen_rules()
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment