0

I dont know why it is not reversing, I am new and would appreciate some help.

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        List = ["h","e","l","l","o"]
        List.reverse()
        
        for i in reversed(List):
            print(i)

and I get this output:

Traceback (most recent call last): File "main.py", line 1, in class Solution: File "main.py", line 2, in Solution def reverseString(self, s: List[str]) -> None: NameError: name 'List' is not defined

  • 1
    `from typing import List` should be at the top of the file – MattDMo Oct 08 '20 at 21:53
  • Then after you do that, change the name of your `List` variable to something else – MattDMo Oct 08 '20 at 21:57
  • If you're supposed to be reversing a list of letters being passed to the class method, why are you using `["h","e","l","l","o"]` in the body of the method instead of the passed value `s`? – MattDMo Oct 08 '20 at 22:02
  • well, i didn't know i was supposed to do that but thx for the help anyway! – Tyson Boring Oct 08 '20 at 22:07
  • No prob. Usually the point of writing a function (or a method in this case) is so you can do the same manipulation/computation on different data. Yes, there definitely is a place for one-off scripts that just work on a single defined data set (I've written plenty myself), but the point of object-oriented programming and classes is so you have *reusable* code that can be put to use over and over again. If your teacher/prof didn't tell you that, hopefully they will before too long. – MattDMo Oct 08 '20 at 22:17

0 Answers0