def find_longest(input_string, start, end, final_list):
letter_dict = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
if start == end:
print("Are we ever getting here?")
largest_len = 1
for sub in final_list:
if len(sub) > largest_len:
largest_len = len(sub)
print(largest_len)
return largest_len
else:
i = start
large_sub = []
while i is not end:
letter = input_string[i]
if letter_dict[letter] == 0:
large_sub.append(letter)
letter_dict[letter] = 1
i += 1
else:
i += 1
final_list.append(large_sub)
find_longest(input_string, start+1, end, final_list)
input_string = "abcabccbb"
start = 0
end = len(input_string)-1
final_list = []
longest = find_longest(input_string, start, end, [])
print(longest)
Asked
Active
Viewed 15 times
0

Charles Duffy
- 280,126
- 43
- 390
- 441

Sebastien Boulas
- 21
- 1
-
1What's with all the whitespace? Vertical whitespace in the wrong places stops a function from being pasted into the Python REPL, making it harder to test. – Charles Duffy Mar 02 '20 at 23:55
-
1That said, the immediate issue is that when you recurse, you need to return the value that your child returns to you, or else it gets thrown away and you just return `None`, as *all* Python functions do by default. – Charles Duffy Mar 02 '20 at 23:56
-
Edited to fix the whitespace. Re: the actual question, change `find_longest(input_string, start+1, end, final_list)` to `return find_longest(input_string, start+1, end, final_list)` -- which is to say, add a `return` at the start of that line. See this working properly at https://ideone.com/gEvGvl – Charles Duffy Mar 03 '20 at 00:11