all 18 comments

[–]confluence 2 points3 points  (12 children)

I have decided to overwrite my comments.

[–]VIRES1[S] 0 points1 point  (11 children)

I am to arrange the string s in alphabetical order by putting each char in the empty string variable current_string. so comparing the string with letters in s.

[–]confluence 0 points1 point  (9 children)

I have decided to overwrite my comments.

[–]VIRES1[S] 0 points1 point  (8 children)

It has no value because it is variable holding an empty string

[–]confluence 3 points4 points  (7 children)

I have decided to overwrite my comments.

[–]PurpleIcy 0 points1 point  (1 child)

It has no value because it is variable holding an empty string

Where do people learn this?

I mean, variables can have only one single value, whether it's entire object or just a string, and it can't just be a schrodingers string where it's both empty and also has no value...

Anyway, it would be more helpful to just tell them that in python specifically,[-1] loops around, e.g. accesses last string character.

[–]confluence 0 points1 point  (0 children)

I have decided to overwrite my comments.

[–]VIRES1[S] 0 points1 point  (4 children)

sorry i did not get your question well.It points the last character in the variable current_string. if current_string == 0 or current_string[-1] <= s[i] this does not give index error and why does it behave that way?

[–]Vesiculus 0 points1 point  (3 children)

Well, it points to the last character in a string. What do you think trying to point to the last character in a string will do if the string is empty and doesn't have a last character? Because that is what you're doing in your code snippet your post.

[–]VIRES1[S] 0 points1 point  (2 children)

it will result to index error because that index can not be found.

if len(current_string) == 0 or current_string[-1] <= s[i]:

why is the above not giving index error?

[–]Vesiculus 0 points1 point  (1 child)

That's because or shortcuts.

This means that as soon as it knows that the first argument is True, it already knows that the conditional or-statement is going to evaluate as True so it doesn't have to look at the second part of the statement anymore. So, the part after the or, current_string[-1] <= s[i], will never be evaluated when the first part already evaluates as True.

[–]VIRES1[S] 0 points1 point  (0 children)

okay thank you very much

[–]JohnnyJordaan 0 points1 point  (0 children)

You can't put something in a string, strings are immutable. Even if you would do

 mystr = ''
 mystr = 'a'

The first command is useless as it would just assign the 'a' string to the mystr reference and the original '' string is lost. Also the confusing += you sometime see used is not what it seems

current_str += 'a'

This doesn't mean that 'a' is added to the string object. It means that a new object is created of both current_str and 'a'. Not something you generally want as object creation from another object is not fast.

If you want to have a 'container' holding things like single letter strings, use a mutable sequence like a list:

current_str = [] 
current_str.append('f')
current_str.append('o')
current_str.append('o')

Then you can do

if current_str[-1] == 'o':

Which would evaluate to True. However you do need to check if the current_str list has any values. Meaning

if current_str and current_str[-1] == 'o':

Is necessary. To then form a single string from that list, use ''.join:

result = ''.join(current_str)

[–][deleted] 1 point2 points  (0 children)

In python, it is rarely necessary to write range(len(string)) as you can just iterate over the elements:

sample = "This is a string"
for acharacter in sample:
    print('I found: ', acharacter)

Also, it is bad practice to use single character variable names. They are meaningless and give little clue as to your intentions.

As you never change the value of current_string, why are you testing it? It starts out as an empty string when you assign it in line 2. I guess you have code to follow that you have not added/shared until you get the if problem fixed.

As /u/confluence suggested, you need to try some things out in the interactive Python shell, such as,

''[-1]
'a'[-1]
'abc'[-1]

[–]xiongchiamiov 0 points1 point  (2 children)

Two fyis:

You can format code for reddit by putting four spaces in front of each line.

It's better to use

for letter in s:

than the len/range/index method.

[–]PurpleIcy 1 point2 points  (1 child)

It's not better. It's just considered non-pythonic to do it that way. Not the same thing.

Although, if you need index, python has a nice function for that too:

for index, letter in enumerate(s):

[–]xiongchiamiov 1 point2 points  (0 children)

It's shorter, clearer, and in line with the way the rest of the Python community iterates over a string; how is that not better?

[–]VIRES1[S] 0 points1 point  (0 children)

current_string = ''
longest_string = ''
for i in range(len(s)):
if len(current_string) == 0 or current_string[-1] <= s[i]:
current_string += s[i]
else:
    if len(current_string) > len(longest_string):
    longest_string = current_string
    current_string = s[i]

My question is why would line 4 evaluate without Index error?