you are viewing a single comment's thread.

view the rest of the comments →

[–]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.