-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpancake-sorting-problem.py
More file actions
executable file
·74 lines (60 loc) · 1.57 KB
/
pancake-sorting-problem.py
File metadata and controls
executable file
·74 lines (60 loc) · 1.57 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
#
# Find the shortest route between input and output given the pancake
# sorting problem.
#
import sys
data = {}
strLen = 0
queue = {}
queueLevel = 0
startStr = ""
endStr = ""
def doFlip(inStr,loc):
outStr = inStr[loc::-1] + inStr[loc+1::]
data[inStr][outStr] = 1;
def doAllFlips(inStr):
for i in range(1,strLen):
doFlip(inStr,i)
for item in data[inStr]:
if item not in data:
data[item] = {}
doAllFlips(item)
def doBreadthFirstSearch(startStr, endStr):
# Convert data to adjacency list format graph
graph = {}
for k in data:
a = []
for item in data[k]:
a.append(item)
graph[k] = a
print bfs(graph, startStr, endStr)
# Breadth first search
# adapted from http://bit.ly/12CpMe7
def bfs(g, s, e):
a = []
a.append([s])
while a:
p = a.pop(0)
n = p[-1]
if n == e:
return p
for adj in g.get(n, []):
np = list(p)
np.append(adj)
a.append(np)
def run(startStr, endStr):
data[startStr] = {}
doAllFlips(startStr)
doBreadthFirstSearch(startStr, endStr)
if __name__ == "__main__":
if len(sys.argv) < 3:
print "Usage: pancake-sorting-problem.py startStr endStr"
sys.exit(1)
startStr = sys.argv[1]
endStr = sys.argv[2]
if len(startStr) != len(endStr):
print "String lengths do not match (%i) (%i)" % (len(startStr),len(endStr))
sys.exit(1)
strLen = len(startStr)
run(startStr, endStr)