"Livre pour former les compétences de programmation pour combattre dans le monde" Exemple de réponse de code Python --1.5 Conversion en un coup
def oneEditAway(first,second):
if len(first) == len(second):
return oneEditReplace(first,second)
elif len(first)+1 == len(second):
return oneEditInsert(first,second)
elif len(first)-1 == len(second):
return oneEditInsert(second,first)
return False
def oneEditReplace(s1,s2):
foundDifference = False
for i in range(len(s1)):
if s1[i] != s2[i]:
if foundDifference:
return False
foundDifference = True
return True
def oneEditInsert(s1,s2):
index1 = 0
index2 = 0
while index2 < len(s2) and index1 < len(s1):
if s1[index1] != s2[index2]:
if index1 != index2:
return False
index2 = index2 + 1
else:
index1 = index1 + 1
index2 = index2 + 1
return True
input_str_1 = "pale"
input_str_2 = "ple"
print(oneEditAway(input_str_1,input_str_2))
input_str_3 = "pales"
input_str_4 = "pale"
print(oneEditAway(input_str_3,input_str_4))
input_str_5 = "pale"
input_str_6 = "bale"
print(oneEditAway(input_str_5,input_str_6))
input_str_7 = "pale"
input_str_8 = "bake"
print(oneEditAway(input_str_7,input_str_8))
Recommended Posts