Created
November 1, 2022 18:59
-
-
Save jalcoding8/68af1d50d467fe6a541971400eacf712 to your computer and use it in GitHub Desktop.
Coding Challenge - Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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". |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['Codecademy', 'rules'] | |
['rules', 'Codecademy'] | |
rules Codecademy | |
ALL TESTS PASS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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