0

I am trying to write code that will do as the title says. If two substrings are the same size, simply print the first string

for example:

s = 'abcbcd'

would print out

'abc'

Here is what I have so far:

old_order = ''
re = ''

for r in range(len(s)):
    order = ''
    for letter in s[r:]:
        try:
            if letter <= s[s.index(letter)+1]:
                order += letter 
        except:
            order += letter
        else:
            order += letter
            print(order)
            if r is 0:
                old_order = order
            if len(order) > len(old_order):
                re = order

            else:
                re = old_order
print(re)

what I am getting:

aabbcbbc
b4hand
  • 9,550
  • 4
  • 44
  • 49
dustinyourface
  • 313
  • 1
  • 3
  • 9
  • 1
    Presumably it doesn't work, or you wouldn't be asking here, but ... what does or doesn't it do? – TessellatingHeckler Jun 22 '15 at 05:48
  • What is the problem you are having? – Loocid Jun 22 '15 at 05:49
  • http://stackoverflow.com/questions/24360166/longest-string-in-alphabetic-order http://stackoverflow.com/questions/11477998/finding-longest-string-in-numberical-order-in-a-string-in-python http://stackoverflow.com/questions/21951203/why-is-the-following-python-code-wrong http://stackoverflow.com/questions/19562340/comparing-characters-in-a-string-sequentially-in-python http://stackoverflow.com/questions/30922659/how-to-return-alphabetical-substrings http://stackoverflow.com/questions/30848311/python-2-7-slice – TessellatingHeckler Jun 22 '15 at 05:55

3 Answers3

0
def isInAlphabeticalOrder(word):
    preString=''
    alphaString=''
    for char in word:
        if(alphaString==''):
            alphaString=char
        elif(alphaString[-1]<=char):
            alphaString+=char
        elif(alphaString[-1]>char):
           if(len(preString)<len(alphaString)):
               preString=alphaString
               alphaString=char
           else:
               alphaString=char
    if(len(alphaString)>len(preString)):
        preString=alphaString
    print(preString)

EDIT: To elaborate on why your code isn't producing the right results - You are appending order with the letter regardless of the outcome in the try statement. Thus, if try statement returns true, letter is appended twice, if not, letter is still appended. Edit: fixed code

user3636636
  • 2,409
  • 2
  • 16
  • 31
0

Code:

def longest_sub_str(A):
    final_res = ""
    tmp = ""
    prev = ""
    for i in A:
        if prev < i:
            prev = i
            tmp += i
        else:
            prev = ""
            if len(tmp)>len(final_res):
                final_res = tmp
            tmp = ""
    return tmp if len(tmp)>len(final_res) else final_res


print longest_sub_str('abcdddabcddefbcd')

Output :

abcd
Kavin Eswaramoorthy
  • 1,595
  • 11
  • 19
-1

Try with this (finding the longest substring of letters in alphabetical order in a given string):

s = 'abcbcd'
r = []
substring = []
for length in s:
    r = r + [length]        
    if r == sorted(r) and len(r) >= len(substring):
        substring=r
    elif r != sorted(r):
        r = [r[len(r)-1]]        

print(substring)

Look at the answer of timgeb in the link I attached for a full answer.

Community
  • 1
  • 1
VinsanityL
  • 810
  • 9
  • 18