Skip to content

Instantly share code, notes, and snippets.

@jalcoding8
Created November 1, 2022 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalcoding8/68af1d50d467fe6a541971400eacf712 to your computer and use it in GitHub Desktop.
Save jalcoding8/68af1d50d467fe6a541971400eacf712 to your computer and use it in GitHub Desktop.
Coding Challenge - Python3
Reverse Words
Write a function, word_reverser(), that will take a given string and reverse the order of the words.
You may assume that the string is a sentence that contains only letters and spaces, with all words separated by one space.
For example, word_reverser("Codecademy rules") should return "rules Codecademy"
and word_reverser("May the Fourth be with you") should return "you with be Fourth the May".
['Codecademy', 'rules']
['rules', 'Codecademy']
rules Codecademy
ALL TESTS PASS
def word_reverser(phrase):
#taking the input string/sentence
#and converting it into a list of strings
word_list = phrase.split(' ')
print(word_list)
#taking the new list and reverseing it
reverse_phrase = word_list[::-1]
print(reverse_phrase)
#converting the reversed list back to string
return (' ').join(reverse_phrase)
print(word_reverser('Codecademy rules'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment