Python: Recursion Practice Computing the largest substring starting with a substring
Problem:
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
Given a string and a non-empty substring sub, compute recursively the largest substring which starts and endsI'm not very satisfied with submy code and return its lengthI'm not sure how I can improve it.
sub_sandwich("catcowcat", "cat") → 9
sub_sandwich("catcowcat", "cow") → 3
sub_sandwich("cccatcowcatxx", "cat") → 9
Code:
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()
I'm not very satisfied with my code and I'm not sure how I can improve it. Your help is appreciated. :)
Python: Recursion Practice
Problem:
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") → 9
sub_sandwich("catcowcat", "cow") → 3
sub_sandwich("cccatcowcatxx", "cat") → 9
Code:
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()
I'm not very satisfied with my code and I'm not sure how I can improve it. Your help is appreciated. :)
Computing the largest substring starting with a substring
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()
Problem:
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") → 9
sub_sandwich("catcowcat", "cow") → 3
sub_sandwich("cccatcowcatxx", "cat") → 9
Code:
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()
if name=="main":main()
I'm not very satisfied with my code and I'm not sure how I can improve it. Your help is appreciated. :)
Problem:
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") → 9
sub_sandwich("catcowcat", "cow") → 3
sub_sandwich("cccatcowcatxx", "cat") → 9
Code:
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()
I'm not very satisfied with my code and I'm not sure how I can improve it. Your help is appreciated. :)
Problem:
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") → 9
sub_sandwich("catcowcat", "cow") → 3
sub_sandwich("cccatcowcatxx", "cat") → 9
Code:
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()
I'm not very satisfied with my code and I'm not sure how I can improve it. Your help is appreciated. :)
Python: Recursion Practice
Problem:
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") → 9
sub_sandwich("catcowcat", "cow") → 3
sub_sandwich("cccatcowcatxx", "cat") → 9
Code:
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()
I'm not very satisfied with my code and I'm not sure how I can improve it. Your help is appreciated. :)