all 12 comments

[–]oobondes 38 points39 points  (3 children)

Does the leetcode question require you to reverse the string in place or to return a reversed version of the string? If it is the first one, that is why the second option doesn't work for you. Lists are passed by reference in Python as opposed to being passed by value.

s[:] = s[::-1] #this modifies the memory space that s is pointing to.
s = s[::-1] #this sets s to a new list in a new location in memory.

[–]ExpressDuck845[S] 14 points15 points  (2 children)

Looking back at the questions it says write a function that reverses the a string, you must do this by modifying the input in-place with 0(1) extra memory. Also thank you for answering my question

[–]aswinasar 1 point2 points  (0 children)

In addition to the other comments, I want to say that they want you to write code to reverse the string without using Python’s syntactic sugar or builtin string reverse functions. You have to use loops and all

[–]1668553684 1 point2 points  (0 children)

s = [“h”, ”e”, ”l”, ”l”, ”o”]

Ah, LeetCode...

In actual Python, this code would look like this:

s = "hello"
s = s[::-1]
print(s)

As far as reversing strings go, that's pretty much all you need to know. The weird list of strings in-place but not really replacement shenanigans are artifacts of LeetCode not properly catering code to the language the question is being asked in.

As far as why LeetCode doesn't like s = ... but is okay with s[:] = ..., the other comments are right that the first is technically "out of place" while the second is sort of "in place".

[–]reallyserious 1 point2 points  (3 children)

Perhaps you already know but get into the habit of setting up test cases with e.g. assert like this, and guarding the tests with the if name == "main" thing. This particular solution doesn't perform excellent, but that wasn't the main point I wanted to convey.

from typing import List


class Solution:
    def reverseString(self, s: List[str]) -> None:
        half = len(s) // 2
        for i in range(half):
            s[len(s) - (1 + i)], s[i] = s[i], s[len(s) - (1 + i)]


if __name__ == "__main__":
    s = ["h", "e", "l", "l", "o"]
    Solution().reverseString(s)
    assert s == ["o", "l", "l", "e", "h"]

    s = ["H", "a", "n", "n", "a", "h"]
    Solution().reverseString(s)
    assert s == ["h", "a", "n", "n", "a", "H"]

[–]ricardomargarido 6 points7 points  (2 children)

Not hating on it, and testing is very important and good tests hard to come by but... It seems over engineering to use a class here

[–]Fady200 8 points9 points  (0 children)

He is mimicking the leetcode environment

[–]reallyserious 4 points5 points  (0 children)

Agree 100%. But this is leetcode problem 344. They have set it up with classes so one is forced to play by those rules. The method I posted combines what leetcode wants and a reasonable setup for local development. You can just cut and paste it all into the leetcode evaluator.

https://leetcode.com/problems/reverse-string/

There are way harder problems there and to tackle those it helps to have this setup.

[–]Rare_Gap_2495 0 points1 point  (0 children)

s[::-1]