- Published on
Reverse string
- Authors
- Name
- Alex Noh
Introduction
reversing a string can be tricky in other programming languages. Let's see how easy and efficeint can this operation be in Python.
Problem
Write a function that reverses a list of characters. You have to manipulate the original string withoout any return
statement.
Things you should know
Two pointers technique
Assume there is a variable, that indicates a position(index) in a list or an array.
You can call this variable a pointer
, because this variable can be viewed as 'pointing' to a certain element in a list.
Obviously, in 'two pointers technique', there are two variables that stores positions of a list.
You can manipulate these variables for various tasks.
Swapping values of two variables
Instead of introducing a temporary variable, you can directly exchange values of multiple variables like this.
main.py
a = 1
b = 2
a, b = b, a
print(a) # 2
print(b) # 1
Solutions
1. Two pointers
main.py
from typing import List
def reverse_string(s: List[str]) -> None:
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
2. Pythonic way
main.py
from typing import List
def reverse_string(s: List[str]) -> None:
s.reverse()