2

The point of the excerise I am doing is to write a program 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:

Longest substring in alphabetical order is: beggh

In the case of ties, print the first substring. For example, if s = 'abcbcd', then your program should print:

Longest substring in alphabetical order is: abc

I have run tests and it works on most of the strings I put in, but this one is problematic.

On step 15, it should compare 2 > 0 which is true, but it does not execute, can someone explain this? Where am I making a mistake?

My code looks like this:

s = 'zodworqozid'
curstring = [0]
longest = [0]
for i in range(1,len(s)):
    if s[i] >= str(curstring[-1]):
        curstring+= s[i]
        if len(curstring) > len(longest):
            longest = curstring
    else:
        curstring = s[i]

print('Longest substring in alphabetical order is:', longest)

I am using python tutor to help visualize the steps

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
Jurkis
  • 23
  • 3

3 Answers3

0

Bug 1:

curstring = [0]

You'll want to initialize your curstring with the first character in s (instead of a list containing the integer 0). I see that you already know how to do this in your code. (i.e. s[0]) This is so that your subsequent code will correctly compare the next character to the first character in s.

Bug 2:

longest = [0]

You'll want to initialize longest as an empty list str (i.e. "") instead of a list containing integer 0. This is because you don't need list functionality in longest as you're directly returning a str.

Bug 3:

longest = curstring

You'll want to make a copy of curstring instead of linking longest to curstring. Do this with either list(curstring) or curstring[:]. No bug here. I misread curstring as a list when it's a str.

ooknosi
  • 374
  • 1
  • 2
  • 8
0

Here:

curstring = [0]
longest = [0]

you are creating two lists with one element inside, which is an integer (0).

Later, here:

if s[i] >= str(curstring[-1]):

you are checking if a given letter has a higher value than the last element on the list. As Python is zero-based, you start from the letter 'o' (because it has index 1, which happens to be the first one in your range list).

for i in range(1,len(s)):

Char (letter) comparision is based on ASCII table, thus any letter given would have a "higher value" than 0 ('o' is 111).

Because the condition from the second block of code is true, the letter 'o' is added to the list in this block:

curstring+= s[i]

and after that, the list curstring has two elements on it: 0 and 'o'.

if len(curstring) > len(longest):

Here you're comparing curstring (described above) and longest which is a list with one element in it: 0 (as you created the list with one element in the first block of code described above). The condition is true (2 elements is more than one element), so longest now points to the same list as curstring and thus has two elements (and the length = 2).

Later on, as you can see in the visualiser you provided, the curstring reference variable doesn't point to the list. It's type is changed when this line is executed:

curstring = s[i]

so later, when a char is added, when the 15. step is executed it turns out to be a two letter string "dw". That string is compared with the list longest with two elements (which was described above). The String "dw" is of length 2 and the list is of length 2, that's why the condition is false.

You should read a bit about types in Python and how the variables are created and maintained in the code - it would help you to catch those small mistakes. Bear in mind that Python has a different syntax when it comes to lists than languages like C++ or Java - I assume that you have written that code

curstring = [0]
longest = [0]

based on experiance with arrays/lists in other languages. Empty list in Python is created like this:

new_list = []
Jonasz
  • 1,617
  • 1
  • 13
  • 19
0

I am not sure what exactly do you mean by "On step 15, it should compare 2>0 which is true, but it does not execute, can someone explain this? where am i making a mistake?". But look at your code,

curstring = [0]
if s[i] >= str(curstring[-1]):

These two statements mean when i==1, Condition s[i] >= str(curstring[-1]) is always true, which is not right in when s[1]

To correct it. Set initial value of curstring to s[0], "curstring = s[0]" will work. One more suggestion. I think "if s[i]>= s[i-1]" is more readable.

Hope this will help.

Kemin Li
  • 1
  • 1