Reverse strings

Énoncé

Reverse the strings of a sentence.

Procédure

  1. Retrieve the sentence that is in argument
  2. Remove spaces with .split()
  3. Use the reverse method
  4. Return the sentence

Schéma

reverse string schema

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.

Replit

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.

Replit