0

Say i have a random string s = 'abcdlfkdf'and i want to grab the largest substring thats in alphabetical order (Problem that has been solved in here before). But i want to learn how to get to the solution and how to inmerse my mind into the comparisons. Note that this is a small fraction of what the latter code would be, i just can't get my head around this small initial part.

My first initial crack at the problem was the following:

s = 'abcdlfkdf'
sub= ''
for i in s:
    if s[0] < s[i+1]:
       #Can't compare integers with strings

So obviously you can't compare integers with strings so you can't really compare individual character strings using index's. What is the alternative?

Can someone point me in the fundamental direction, would really want to solve this without looking anything remotely close to a solution. My mind needs to suffer through this.

Thanks!

skaffman
  • 398,947
  • 96
  • 818
  • 769
user40720
  • 89
  • 1
  • 2
  • 9

2 Answers2

0

Change

for i in s:

to:

for i in range(len(s)):
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • Thanks! However when i try to append s[i] to sub when it meets the condition, python then yields an Index Error "String index out of range" – user40720 Jan 08 '15 at 20:50
  • Print out that value of `i` just before that error and tell us what it is. – Saqib Ali Jan 08 '15 at 20:53
  • it yields integers 0 to 8. Interestingly enough, when i run the same code in PythonTutor it appends to sub just fine using the sub+=s[i] command once it meets the if condition. – user40720 Jan 08 '15 at 21:05
0

I'd say the best hint i could give you would be to look at the value of the character either in Unicode or in ASCII :)

You could use two different functions. Referencing this:

function ord() would get the int value of the char. And in case you want to convert back after playing with the number, function chr() does the trick.

Unichar() is the other function you could use. Does this hint towards it enough? :)

My attempt:

    s = 'abcdlfkdf'
    print("Our original string is " + str(s))
    bestindex='0'
    bestlength=0
    for i in range(len(s)-2):
        best = 0
        m = i
        while(ord(s[m]) < ord(s[m+1]) and m<len(s)-2):
            best += 1
            m += 1
        if best > bestlength:
            bestlength = best
            bestindex=i
    print("\n and the longest alphabetical substring is '" + s[bestindex:-(len(s)-bestlength-1)] + "'!")
Community
  • 1
  • 1
Nico
  • 399
  • 1
  • 15
  • Saqib Ali's answer may have been what you were looking for though hehe – Nico Jan 08 '15 at 20:23
  • Thanks for the prompt input though, watch for my next response in Saqib Ali's. – user40720 Jan 08 '15 at 20:44
  • If you are doing i+1, there will be a conflict since the last index + 1 doesn't exist. Are you just trying to make sub hold the longest alphabetical string? I hope this doesn't give you too much... but: You could just store the index of said string and the length of it. Then you can see if as you go through the string, you can find a different substring that is also alphabetical and a greater length. Tell me if I'm still off of your conflict :P – Nico Jan 09 '15 at 22:15
  • I am trying to get sub to hold the longest alphabetical string. You are doing good :) How would the length help me at all? – user40720 Jan 10 '15 at 03:32
  • You would be able to compare the greatest one found *so far* with the "rest". Basically, if you think about your original scenario: 'abcdlfkdf' You would find 'abcdl' first. Then as you kept going, you would find 'fk'. It's in alphabetical order, but it's length is 2 rather than 5... so you know that 'abcdl' is still the better option. This the kind of idea I'm suggesting hehe – Nico Jan 10 '15 at 22:30
  • Got ya! That would be the last piece of the puzzle. Im stuck in the intermediate. I have figured how to compare individual character strings without going past the range. However, I am trying to draw the line in the sand of just making sure the first substring is created. I have used s[i-1] > s[i] but this always misses one character. – user40720 Jan 10 '15 at 23:40
  • I didn't want to do it but it's probably easiest this way... I've posted the "answer". Maybe it will be more clear :) – Nico Jan 11 '15 at 00:06