Reputation: 13
I'm new to coding in general and have been studying through the edX platform. One of the first assignments require us to write a piece of code that prints the longest alphabetical substring in a given string.
I have tried looking everywhere on the internet, but all I can find is fixed answers, and not why my code doesn't work. I would like some help to figure out why, because it makes sense in my head.
CODE:
s = 'abcdszabc'
start = 0
end = 0
a = ''
b = ''
while len(b) < len(s[start:]):
while s[end] <= s[end+1]:
end += 1
a = s[start:end+1]
if a > b:
b = a
print(b)
start += 1
end = start
When I run it retrieves an error claiming:
while s[end] <= s[end+1]:
IndexError: string index out of range
It doesn't make sense, because the first while should assess the size of the b string before going into the second while. In my case, the b would be assigned 'abcdsz' in the first try, and the program should stop looping in the first while when there is less than 6 characters left (since there is no point in further searching).
Can anyone please enlighten me without giving me a straight solution? I'm trying my best to solve it on my own without spoilers!
Upvotes: 1
Views: 102
Reputation: 77910
Applying basic tracing to your code ...
while len(b) < len(s[start:]):
print ("WHILE 1", s, end)
while s[end] <= s[end+1]:
end += 1
print ("WHILE 2", s, end)
a = s[start:end+1]
if a > b:
b = a
print("RESULT", b)
start += 1
end = start
Output:
WHILE 1 abcdszabc 0
WHILE 2 abcdszabc 1
WHILE 2 abcdszabc 2
WHILE 2 abcdszabc 3
WHILE 2 abcdszabc 4
WHILE 2 abcdszabc 5
RESULT abcdsz
WHILE 1 abcdszabc 1
WHILE 2 abcdszabc 2
WHILE 2 abcdszabc 3
WHILE 2 abcdszabc 4
WHILE 2 abcdszabc 5
RESULT bcdsz
WHILE 1 abcdszabc 2
WHILE 2 abcdszabc 3
WHILE 2 abcdszabc 4
WHILE 2 abcdszabc 5
RESULT cdsz
WHILE 1 abcdszabc 3
WHILE 2 abcdszabc 4
WHILE 2 abcdszabc 5
RESULT dsz
WHILE 1 abcdszabc 4
WHILE 2 abcdszabc 5
RESULT sz
WHILE 1 abcdszabc 5
RESULT z
WHILE 1 abcdszabc 6
WHILE 2 abcdszabc 7
WHILE 2 abcdszabc 8
Traceback (most recent call last):
File "so.py", line 9, in <module>
while s[end] <= s[end+1]:
IndexError: string index out of range
There it is: end
is 8, so end+1
is 9. Indices run only 0-8 for your 9-character string. When the substring you're checking is in alphabetic order, you have no control to keep the search from running off the right end.
Upvotes: 1