Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for String Conversion by Rounin
def findLongestMatch(line1, line2):
line1 = list(line1)
line2 = list(line2)
longestMatch = 0
start_line1 = 0
end_line1 = 0
start_line2 = 0
end_line2 = 0
for i in range(len(line1)):
s=0
e=0
while s longestMatch):
longestMatch = e
start_line1 = i
start_line2 = s
end_line1 = i+e
end_line2 = s+e
return [start_line1, end_line1, start_line2, end_line2, longestMatch]
# Remove the longest match recursively until we're left with only the differences.
def removeLongestMatch(line1, line2):
longestMatch = findLongestMatch(line1, line2)
if(longestMatch[4] == 0):
return [line1, line2]
else:
line1_left = line1[0:longestMatch[0]]
line1_right = line1[longestMatch[1]:]
line2_left = line2[0:longestMatch[2]]
line2_right = line2[longestMatch[3]:]
longestMatch_left = removeLongestMatch(line1_left, line2_left)
longestMatch_right = removeLongestMatch(line1_right, line2_right)
return [longestMatch_left[0]+longestMatch_right[0],longestMatch_left[1]+longestMatch_right[1]]
# Remove all common parts of the lines, and count the changes that are left.
def steps_to_convert(line1, line2):
lines = removeLongestMatch(line1, line2)
return max(len(lines[0]), len(lines[1]))
if __name__ == "__main__":
#These "asserts" using only for self-checking and not necessary for auto-testing
assert steps_to_convert('line1', 'line1') == 0, "eq"
assert steps_to_convert('line1', 'line2') == 1, "2"
assert steps_to_convert('line', 'line2') == 1, "none to 2"
assert steps_to_convert('ine', 'line2') == 2, "need two more"
assert steps_to_convert('line1', '1enil') == 4, "everything is opposite"
assert steps_to_convert('', '') == 0, "two empty"
assert steps_to_convert('l', '') == 1, "one side"
assert steps_to_convert('', 'l') == 1, "another side"
print("You are good to go!")
July 22, 2017