Question

Okay I have questions regarding the following code:

s = "wxyabcd"

myString = s[0]
longest = s[0]
for i in range(1, len(s)):
    if s[i] >= myString[-1]:
        myString += s[i]
        if len(myString) > len(longest):
            longest = myString
    else:
        myString = s[i]
print longest

Answer: "abcd" w wx wxy a ab abc abcd

I am new to Python and I am trying to learn how some of these loops work but I am very confused. This found what the longest string in alphabetical order was... The actual answer was "abcd" but I know that the process it went through was one by one.

Question: Can someone please guide me through the code so I can understand it better? Since there are 7 characters I am assuming it starts by saying: "For each item in range 1-7 if the item is 'more' than myString [-1] which is 'w' then I add the letter plus the item in i which in this case it would be 'x'.

I get lost right after this... So from a - z : a > z? Is that how it is? And how then when s[i] != myString[-1] did it skip to start from 'a' in s[i].

Sorry I am all over the place. Anyways i've tried to search places online to help me learn this but some things are just hard. I know that in a few months ill get the hang of it and hopefully be more fluent.

Thank you!

No correct solution

OTHER TIPS

Here's a bit of an explanation of the control flow and what's going on with Python's indexing, hope it helps:

s = "wxyabcd"

myString = s[0] # 'w'
longest = s[0] # 'w' again, for collecting other chars
for i in range(1, len(s)): # from 1 to 7, exclusive of 7, so 2nd index to last
    if s[i] >= myString[-1]: # compare the chars, e.g. z > a, so x and y => True
        myString += s[i] # concatenate on to 'w'
        if len(myString) > len(longest): # evident?
            longest = myString # reassign longest to myString
    else:
        myString = s[i] # reassign myString to where you are in s.
print longest
# s is a 7 character string
s = "wxyabcd"

# set `mystring` to be the first character of s, 'w'
myString = s[0]

# set `longest` to be the first character of s, 'w'
longest = s[0]

# loop from 1 up to and not including length of s (7)
# Which equals for i in (1,2,3,4,5,6):
for i in range(1, len(s)):

    # Compare the character at i with the last character of `mystring`
    if s[i] >= myString[-1]:

        # If it is greater (in alphabetical sense)
        # append the character at i to `mystring`
        myString += s[i]

        # If this makes `mystring` longer than the previous `longest`,
        # set `mystring` to be the new `longest`
        if len(myString) > len(longest):
            longest = myString

    # Otherwise set `mystring` to be a single character string
    # and start checking from index i
    else:
        myString = s[i]

# `longest` will be the longest `mystring` that was formed,
# using only characters in descending alphabetic order
print longest

Two approaches I can think of (quickly)

def approach_one(text): # I approve of this method!
    all_substrings = list()
    this_substring = ""
    for letter in text:
        if len(this_substring) == 0 or letter > this_substring[-1]:
            this_substring+=letter
        else:
            all_substrings.append(this_substring)
            this_substring = letter
    all_substrings.append(this_substring)
    return max(all_substrings,key=len)

def approach_two(text):
    #forthcoming
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top