简体   繁体   中英

Longest string in alphabetic order

This code works fine for all the strings, except for the ones where the last char is needed.

s='abcdefghijklmnopqrstuvwxyz'
sub =''
test =s[0]

for n in range(len(s)-1):
    if len(test) > len(sub):
        sub = test
    if s[n] >= s[n-1]:
        test += s[n]
    else:
        test = s[n]

print 'Longest substring in alphabetic order is: ' + str(sub) 

How do you suggest a possibility for doing this?

Thank you in advance guys!

PS:

Thanks for the answers so far. The problem is that, no matter which range I type, the sub variable, which I will print, doesn't get all the chars I want. The loop finishes before :\\ Maybe it's a problem with the program itself.

Any extra tips? :)

Your problem is with range(len(s)-1) . Range generates a list up to its upper limit parameter value-1 so you don't need to subtract1 to len(s), use:

range(len(s))

From https://docs.python.org/2/library/functions.html#range

range(stop) range(start, stop[, step]) This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero

On the other hand, you are labeling your question as python2.7 so I assume you are using 2.7. If that is the case, it is more efficient to use xrange instead range because that way you will use an iterator instead generating a list.

EDIT

From the comments to this question, you can change your code to:

s='caabcdab'
sub =''
test =s[0]

for i in range(1,len(s)+1):
    n = i%len(s)
    if len(test) > len(sub):
        sub = test
        if i >= len(s):
            break
    if s[n] >= s[n-1]:
        test += s[n]
    else:
        test = s[n]

print 'Logest substring in alphabetic order is: ' + str(sub)

you enemurate instead of range:

 s='abcdefghijklmnopqrstuvwxyz'

sub =''
test =s[0]

for n,value in enumerate(s):
    if len(test) > len(sub):
        sub = test
    if value >= s[n-1]:
        test += s[n]
    else:
        test = s[n]

You could do use the following code:

s = 'abcdefgahijkblmnopqrstcuvwxyz'
sub = ''
test = s[0]

for index, character in enumerate(s):
    if index > 0:
        if character > s[index - 1]:
            test += character
        else:
            test = character
    if len(test) > len(sub):
        sub = test
print 'Longest substring in alphabetic order is: ' + str(sub)

A few pointers too.

  1. Python strings are iterable. ie you can loop through them.
  2. Use enumerate when you want the index of the list while iterating it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM