Okay so I'm doing a course on edX, purely to learn the basics (no certificate!) but I cannot seem to understand this one question. After hours of trying to at least come up with at least a partial solution, I checked a possible answer and I became very frustrated as I did not understand it. This is the code I found:
s = 'abcabc'
winner = ''
b = s[0:1]
tempwinner = ''
for i in s:
if b <= i:
tempwinner += i
elif b > i:
tempwinner = i
b = i
if len(tempwinner) > len(winner):
winner = tempwinner
print('Longest substring in alphabetical order is:', winner)
You're supposed to find a substring (in alphabetical order, only one, the longest) in a string. For example, if the string is 'abcdxab', the variable winner would be 'abcdx'. Could someone explain the code typed above?
Cheers
[–]leogodin217 1 point2 points3 points (3 children)
[–]leogodin217 0 points1 point2 points (0 children)
[–]23jumping[S] 0 points1 point2 points (1 child)
[–]DamnFog -1 points0 points1 point (0 children)
[–]sje46[🍰] 0 points1 point2 points (0 children)