Skip to main content
Code Review

Return to Question

deleted 65 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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") → 9
  • sub_sandwich("catcowcat", "cow") → 3
  • sub_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") → 9
  • sub_sandwich("catcowcat", "cow") → 3
  • sub_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()
code block format fix
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

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. :)

Source Link
Diba SH
  • 101
  • 5

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. :)

lang-py

AltStyle によって変換されたページ (->オリジナル) /