-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmu.py
48 lines (42 loc) · 978 Bytes
/
mu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Chapter 1 The MU-Puzzle
import re
def r1(s):
if s[-1] == 'I':
return [s + 'U']
else:
return []
def r2(s):
if s[0] == 'M':
return [s + s[1:]]
else:
return []
def r3(s):
return [s[:m.start()] + 'U' + s[m.start() + 3:] for m in
re.finditer('(?=III)', s)]
def r4(s):
return [s[:m.start()] + s[m.start() + 2:] for m in
re.finditer('(?=UU)', s)]
fns = [r1, r2, r3, r4]
# Example:
# search('MI', MIIU')
#
# This way madness lies:
# search('MI', 'MU')
def search(axiom, goal):
i = 0
collection = {axiom: []}
while not collection.has_key(goal):
i += 1
print i, 'collection size:', len(collection)
cprime = {}
for s in collection:
path = list(collection[s])
path.append(s)
for fn in fns:
sprimes = fn(s)
for sprime in sprimes:
if sprime == goal:
return path
if not collection.has_key(sprime):
cprime[sprime] = path
collection = cprime