Reputation: 1
What I do not understand is how indexing a string s = "azcbobobegghakl" would correctly give me the answer 'beggh' if I was looking for the longest substring in alphabetical order. How does this code tell the computer that 'beggh' is in alphabetical order? I thought indexing just gave each letter in the string a number that increased by one until the string ended? I tweaked the code a bit and it worked, but I do not actually understand how the code works in the first place.
s = "azcbobobegghakl"
longest = s[0]
current = s[0]
for c in s[1:]:
if c >= current[-1]:
current += c
else:
if len(current) > len(longest):
longest = current
current = c
print "Longest substring in alphabetical order is:", longest
Upvotes: 0
Views: 78
Reputation: 887
s = "azcbobobegghakl"
# initialize vars to first letter in string
longest = s[0]
current = s[0]
# start iterating through rest of string
for c in s[1:]:
# c is the letter of current iteration and current[-1]
# is the last letter of the current string. Every character
# has an ASCII value so this is checking the character's
# numerical values to see if they are in alphabetical order (a<b<c etc.)
if c >= current[-1]:
# so if c is bigger than current[-1] (meaning it comes
# after in the alphabet, then we want to add it to current
current += c
else:
# if c was smaller then we want to check if the alphabetic
# string we just read is longer than the previous longest
if len(current) > len(longest):
longest = current
# start the current string over beginning with the letter in c
current = c
print "Longest substring in alphabetical order is:", longest
Upvotes: 1
Reputation: 87074
The expression c >= current[-1]
lexicographically compares each character with its predecessor in the string. More simply:
>>> 'a' >= 'a'
True
>>> 'b' >= 'a'
True
>>> 'b' >= 'c'
False
>>> 'z' >= 'a'
True
So all the code is doing is to accumulate runs of characters that have each character >=
the last one, and to keep the longest one seen.
Upvotes: 1
Reputation: 1121904
>=
compares two characters by their lexicographical order; the expression
if c >= current[-1]:
tests if c
(a single character), is the same or is 'greater' than current[-1]
(another single character. Strings are ordered lexicographically by their codepoint value; a
is smaller than b
because the character a
comes before b
in the Unicode standard:
>>> 'a' > 'b'
False
>>> 'a' < 'b'
True
As such, characters are only added to current
if they come 'later' in the alphabet than the last character of current
. If they do not, then the sequence so far is tested for length, and a new substring is started from c
.
Upvotes: 1