-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch022_reverse_words.py
More file actions
37 lines (30 loc) · 1.01 KB
/
ch022_reverse_words.py
File metadata and controls
37 lines (30 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def reverse_words(sentence: str) -> str:
"""
DOCSTRING:
Inverte a ordem das palavras em uma frase.
As palavras permanecem intactas, apenas a sequência muda.
EXEMPLO:
>>> reverse_words("hello world")
'world hello'
>>> reverse_words("Python is fun")
'fun is Python'
"""
return " ".join(sentence.split()[::-1])
if __name__ == "__main__":
print("---------- Word Reversal ----------")
print("Digite uma frase para inverter a ordem das palavras.")
print("Exemplo: 'Python is awesome'")
print("Escreva 'sair' para encerrar ou 'help' para ajuda.\n")
while True:
texto = input("> ").strip()
if texto.lower() == "sair":
print("\nEncerrando...\n")
break
elif texto.lower() == "help":
print(reverse_words.__doc__)
continue
if not texto:
print("❌ Digite uma frase válida.\n")
continue
resultado = reverse_words(texto)
print(f"Resultado: {resultado}\n")