You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
daily_programmer/python/easy_word_funnel.py

32 lines
872 B

def funnel_naive(string1, string2):
"""
detect if the second string can be made from the first by removing one letter
"""
if len(string1) - len(string2) != 1:
return False
if string2 in string1:
return True
miss = 0
for i, letter2 in enumerate(string2):
if letter2 != string1[i]:
if miss > 1:
return False
miss += 1
if letter2 == string1[i + 1]:
i += 1
else:
return False
return True
funnel = lambda word, other: other in [w[:i] + w[i+1:] for i, w in enumerate([word] * len(word))]
print(funnel_naive("leave", "leav"))
print(funnel("leave", "eave"))
print(funnel("reset", "rest"))
print(funnel("dragoon", "dragon"))
print(funnel("eave", "leave"))
print(funnel("sleet", "lets"))
print(funnel("skiff", "ski"))