Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

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?

  • 6 months ago
  • 6 months ago

  • This Question is Closed
  1. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    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.

    • 6 months ago
  2. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    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

    • 6 months ago
  3. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    That is basically what it would do. Now for the how, use ifs and boolian comparisons.

    • 6 months ago
  4. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    so i keep creating substrings and comparing lengths, and depending on their lengths i will keep the largest one

    • 6 months ago
  5. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    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 >>>

    • 6 months ago
  6. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    How can I compare it with values from before?

    • 6 months ago
  7. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    Testing each character as you go will let you decide if it is time to reset the buffer string or to add to it.

    • 6 months ago
  8. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    I need to rest.I will come back in some hours

    • 6 months ago
  9. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    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.

    • 6 months ago
  10. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • 6 months ago
  11. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    But this would just work for a continued string...I dont know how it would be done for a new substring

    • 6 months ago
  12. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    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.

    • 6 months ago
  13. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    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.

    • 6 months ago
  14. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    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))

    • 6 months ago
  15. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    |dw:1382852135152:dw|

    • 6 months ago
  16. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    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.

    • 6 months ago
  17. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    why are the results numbers?

    • 6 months ago
  18. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    str(i) is not the same as str[i]

    • 6 months ago
  19. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    ``` >>> s = "this is a test" >>> s[1] 'h' >>> i=1 >>> s[i] 'h' >>> s(i) Traceback (most recent call last): File "<interactive input>", line 1, in <module> TypeError: 'str' object is not callable ```

    • 6 months ago
  20. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    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=str(i) + str(i+1) if s[i]>=s[i+1]: substring="" print("Longest substring in alphabetical order is: " + str(substring))

    • 5 months ago
  21. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    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' >>> ```

    • 5 months ago
  22. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    I have two hours to finish this :/ Can you give me a push?

    • 5 months ago
  23. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    It throws me this :( string indices must be integers, not str

    • 5 months ago
  24. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    s=raw_input("Enter a word: ") substring="" output="" for i in range(len(s)-1): if s[i]<s[i+1]: substring=substring+s[i] if len(substring)>len(output): output=substring else: substring="" print output

    • 5 months ago
  25. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    Forget it, no more time. I still want to achieve this

    • 5 months ago
  26. thisSucks
    Best Response
    You've already chosen the best response.
    Medals 0

    @e.mccormick doen't leave any question unanswered

    • 5 months ago
  27. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    Some possible flow of control issues.

    • 5 months ago
  28. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    Let me check this

    • 5 months ago
  29. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    Forgot to put all the yes/no on that.

    • 5 months ago
  30. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    • 5 months ago
  31. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    I also realized that you need to test the last character in there somewhere, which is why that has another if in it.

    • 5 months ago
  32. osanseviero
    Best Response
    You've already chosen the best response.
    Medals 0

    which is the use of for i=0?

    • 5 months ago
  33. e.mccormick
    Best Response
    You've already chosen the best response.
    Medals 3

    It is a way of showing a for loop in a diagram.

    • 5 months ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.