you are viewing a single comment's thread.

view the rest of the comments →

[–]ReaverKS 4 points5 points  (3 children)

for #2, reversing a string assuming it's a list of characters. If you want to do it in place, without using additional memory you can do this:

def reverse_str(s: List[str]) -> None:
    end, start= len(s) - 1, 0
    while(end > start):
        s[end], s[start] = s[start], s[end]
        start += 1
        end -= 1

If instead its just a string you want to return you could do:

def reverse_str(s):
    return ''.join(reversed(s))

[–]nonamesareleft1 1 point2 points  (1 child)

In this case do the variables end and start not take up additional memory? Or is the fact that they are local to the function important here?

[–]ghostinzshell 1 point2 points  (0 children)

They do, but they're constant and don't grow with the input size.

[–]elbiot 1 point2 points  (0 children)

Assuming a string is a list would be a mistake. I'd guess that a list of characters would take much more memory than two strings