Given a string and a non-empty substring sub, compute recursively the largest substring which starts and ends with sub and return its length.
sub_sandwich("catcowcat", "cat")
→ 9sub_sandwich("catcowcat", "cow")
→ 3sub_sandwich("cccatcowcatxx", "cat")
→ 9
I'm not very satisfied with my code and I'm not sure how I can improve it.
def subSandwich(word, char, pos, start, end):
if pos == len(word)-1:
if end == 0:
return len(char)
return end - (start-2)
if word[pos:pos+len(char)] == char:
if start != 0:
end = pos + len(char) - 1
else:
start = pos+1
return subSandwich(word, char, pos + 1, start, end)
def main():
print subSandwich("catcowcat", "cow",0,0,0)
if __name__ == "__main__":
main()
str.index()
ands.rindex()
. \$\endgroup\$