Reverse strings
Énoncé
Reverse the strings of a sentence.
Procédure
- Retrieve the sentence that is in argument
- Remove spaces with .split()
- Use the reverse method
- Return the sentence
Schéma

Solution 1
1def reverse_string(sentence):
2
3 words = sentence.split(" ")
4
5 result = " ".join(reversed(words))
6 print(result)
Complexité
O(n) : n grows proportionally to the size of the input.
Solution 2
1def reverse_string(sentence):
2
3 words = sentence.split()[::-1]
4
5 new_sentence = []
6
7 for i in words:
8 new_sentence.append(i)
9
10 print(" ".join(new_sentence))
Complexité
O(n) : n grows proportionally to the size of the input.