osanseviero
  • osanseviero
I need to write a program that 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 beggh How should this start?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
e.mccormick
  • e.mccormick
hmm... well, you could look at each character and see if it is <= the next. Count how many are, then make a sub string based on that. The issue is when to discard it if you find a larger string.
e.mccormick
  • e.mccormick
azcbobobegghakl az alphabetical, but len 2. zc bad cb good, but not loneger cbo good, 3, drop the 2. ob bad bo good, but not loneger ob bad be good, but not loneger beg good, but not loneger begg good, loneger drop the 3 beggh good, loneger drop the 4 ha bad ak good but not longer akl good but not longer
e.mccormick
  • e.mccormick
That is basically what it would do. Now for the how, use ifs and boolian comparisons.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

osanseviero
  • osanseviero
so i keep creating substrings and comparing lengths, and depending on their lengths i will keep the largest one
e.mccormick
  • e.mccormick
Well, you only need two bufer strings. The longes one as the answer and the now being worked on one. If the now worked becomes longer than the answer, replace the answer. >>> 'a'<'b' True >>> 'c'<'b' False >>>
osanseviero
  • osanseviero
How can I compare it with values from before?
e.mccormick
  • e.mccormick
Testing each character as you go will let you decide if it is time to reset the buffer string or to add to it.
osanseviero
  • osanseviero
I need to rest.I will come back in some hours
e.mccormick
  • e.mccormick
OK. This should give you something to start with. You can see what it would do and some info on how. Work with that and see how far you get.
osanseviero
  • osanseviero
In human talking, this would be done with this: 1. Take the first character 2. Save it. 3. Take the second character. 4. Compare them Option 1: a. If first character is bigger than the second one, FALSE statement, continue with first character as the substring. b. If first character is smaller, TRUE statement, new substring is first character + second one. 5. Repeat
osanseviero
  • osanseviero
But this would just work for a continued string...I dont know how it would be done for a new substring
e.mccormick
  • e.mccormick
Well, if the first character is bigger, then you want to reset or erase the buffer string. If it is smaller, then you want to add it to the buffer string.
e.mccormick
  • e.mccormick
And after every time through those, you want to see if the buffeer string is longer than the answer string. If so, replace the answer string with the buffer string.
osanseviero
  • osanseviero
Why does this give a number as an answer? s=raw_input("Enter a word: ") buffer="" for i in range(len(s)-1): if s[i]<=s[i+1]: buffer=str(i)+str(i+1) if s[i]>=s[i+1]: buffer=buffer print("Longest substring in alphabetical order is: " + str(buffer))
e.mccormick
  • e.mccormick
|dw:1382852135152:dw|
e.mccormick
  • e.mccormick
buffer=str(i)+str(i+1) buffer=str[i]+str[i+1] You need to use [ ] for string arrays. And you only want to work on one character at a time.
osanseviero
  • osanseviero
why are the results numbers?
e.mccormick
  • e.mccormick
str(i) is not the same as str[i]
e.mccormick
  • e.mccormick
``` >>> s = "this is a test" >>> s[1] 'h' >>> i=1 >>> s[i] 'h' >>> s(i) Traceback (most recent call last): File "", line 1, in TypeError: 'str' object is not callable ```
osanseviero
  • osanseviero
It never gives me an answer :/ s=raw_input("Enter a word: ") substring="" for i in range(len(s)-1): if s[i]=s[i+1]: substring="" print("Longest substring in alphabetical order is: " + str(substring))
e.mccormick
  • e.mccormick
Like I said, you will need two buffers to hold strings. Also, you still have str(i). That is the numbers. You need s[i]. Also, this is how you turn multiple things into one but keep the first thing as part: ``` >>> a = 'abc' >>> b = 'def' >>> c = 'hij' >>> a 'abc' >>> a = a + b + c >>> a 'abcdefhij' >>> ```
osanseviero
  • osanseviero
I have two hours to finish this :/ Can you give me a push?
osanseviero
  • osanseviero
It throws me this :( string indices must be integers, not str
osanseviero
  • osanseviero
s=raw_input("Enter a word: ") substring="" output="" for i in range(len(s)-1): if s[i]len(output): output=substring else: substring="" print output
osanseviero
  • osanseviero
Forget it, no more time. I still want to achieve this
anonymous
  • anonymous
@e.mccormick doen't leave any question unanswered
e.mccormick
  • e.mccormick
Some possible flow of control issues.
osanseviero
  • osanseviero
Let me check this
e.mccormick
  • e.mccormick
Forgot to put all the yes/no on that.
e.mccormick
  • e.mccormick
e.mccormick
  • e.mccormick
I also realized that you need to test the last character in there somewhere, which is why that has another if in it.
osanseviero
  • osanseviero
which is the use of for i=0?
e.mccormick
  • e.mccormick
It is a way of showing a for loop in a diagram.

Looking for something else?

Not the answer you are looking for? Search for more explanations.