Based on the prompt, my input should read "beggh", but my code is for some reason skipping the H. I've tried everything I can think of.
Prompt: 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. Please use s = 'azcbobobegghakl' to test your codes. 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
s = "azcbobobegghakl"
current_string = ''
longest_string = ''
n = 0
def sorting_string(n):
global current_string
global longest_string
if n == (len(s) - 1):
return 0
elif ord(s[n]) <= ord(s[n+1]):
if len(current_string) <= 1:
current_string = current_string + s[n]
return sorting_string(n + 1)
if ord(s[n+1]) > ord(current_string[-1]):
current_string = current_string + s[n]
else:
if ord(s[n]) >= ord(current_string[-1]):
current_string = current_string + s[n]
if len(current_string) > len(longest_string):
longest_string = current_string
else:
current_string = ''
return sorting_string(n + 1)
else:
current_string = ''
return sorting_string(n + 1)
print(s)
sorting_string(n)
print(longest_string)
print(current_string)
print(ord("g"))
print(ord("h"))
output
azcbobobegghakl
begg
ak
103
104