Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. # Problem 3
  2. # 15.0/15.0 points (graded)
  3. # Assume s is a string of lower case characters.
  4.  
  5. # 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
  6.  
  7. # Longest substring in alphabetical order is: beggh
  8. # In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print
  9.  
  10. # Longest substring in alphabetical order is: abc
  11. # Note: This problem may be challenging. We encourage you to work smart. If you've spent more than a few hours on this problem, we suggest that you move on to a different part of the course. If you have time, come back to this problem after you've had a break and cleared your head.
  12.  
  13.  
  14. # ----------------Under line Code --------------------------------#
  15.  
  16. result, current = '',''
  17.  
  18. for alphabet in s:
  19. if (current == ''):
  20. current = alphabet
  21. elif (current[-1] <= alphabet):
  22. current += alphabet
  23. elif (current[-1] > alphabet):
  24. if (len(result) < len(current)):
  25. result = current
  26. current = alphabet
  27. else:
  28. current = alphabet
  29. if (len(current) > len(result)):
  30. result = current
  31.  
  32. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement