49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
import textwrap
|
|
import difflib
|
|
|
|
def onestepchange(start, dest):
|
|
|
|
ret = ""
|
|
|
|
for i, s in enumerate(difflib.ndiff(start, dest)):
|
|
#print("i: ", i)
|
|
#print("S: ", s)
|
|
|
|
# Remove a character from the start.
|
|
if s[0] == '-':
|
|
#print("ret1")
|
|
return ret + start[i+1:]
|
|
|
|
# Add a character.
|
|
if s[1] == '+':
|
|
#print("ret2")
|
|
return ret + s[-1] + start[i:]
|
|
|
|
# Keep moving through the stream.
|
|
ret = ret + s[-1]
|
|
|
|
# If we're at the length of the starting string plus one, then we've
|
|
# added our one character. Let's bounce.
|
|
if len(ret) > len(start):
|
|
#print("ret3")
|
|
return ret
|
|
|
|
|
|
if ret[i] != start[i]:
|
|
#print("ret4")
|
|
return ret + start[i:]
|
|
|
|
# Hack.
|
|
if ret == "":
|
|
return dest
|
|
|
|
#print("ret5 - ret")
|
|
return ret
|
|
|
|
def countsteps(start, dest):
|
|
step_count = 0
|
|
while start != dest:
|
|
start = onestepchange(start, dest)
|
|
step_count += 1
|
|
return step_count
|