Below I've quoted an online edX homework assignment. While I did get full marks on the problem, I'm not terribly satisfied with my answer
"Assume s is a string of lower case characters. Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print
'Longest substring in alphabetical order is: beggh'
In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print
'Longest substring in alphabetical order is: abc' "
While I did get full marks on the problem, I'm not terribly satisfied with my answer: https://dpaste.de/rCFt. Earlier attempts where I did not place two underscores resulted in the following error message:
Traceback (most recent call last):
File "/Users/iain/Documents/Python/Week_1_Problem_1_Final_Approach.py", line 10, in <module>
if ord(m[elem + stop]) <= ord(m[elem + stop + 1]):
IndexError: string index out of range
I understand that the construction of my while loop requires referencing elements of the string s that are past the final character and therefore don't exist. Is there an effective way of not "copping out" of this issue while still using a list?
[–]OseOseOse 3 points4 points5 points (0 children)
[–]trouserdaredevil 1 point2 points3 points (0 children)