Python Challenge - Reverse Words

def word_reverser(phrase): return " ".join(reversed(phrase.split())) print(word_reverser('Codecademy rules'))
def word_reverser(phrase): str_return = "" #definition of empty string l = phrase.split() #sorting of the words into array i = len(l) - 1 #defining index to iterate backwards while i >= 0: #iterating from last element in l to first element str_return += l[i] #adding element corresponding to i to the empty string str_return += " " #don't forget the space i -= 1 #no infinite while loops here! return str_return[0:-1] #gotta make sure to remove the last space added by line 7 print(word_reverser('Codecademy rules'))

This is my solution:

def word_reverser(phrase):
inverted =
phrase_list = phrase.split(" ")
for element in phrase_list[::-1]:
inverted.append(element)
inverted_text = " ".join(inverted)
return inverted_text

print(word_reverser(‘Codecademy rules’))

# Here is my solution: def word_reverser(phrase): splitPhrase= phrase.split(' ') newPhraseArr = [ ] for x in range(len(splitPhrase) - 1, -1, -1): newPhraseArr.append(splitPhrase[ x ]) newPhrase =" ".join(newPhraseArr) return newPhrase print(word_reverser('Codecademy rules'))

I feel like my code can probably be optimised to remove some variables that were not used.

def word_reverser(phrase): listy = [] counter = 0 dicty = {} output = [] for i in range(0, len(phrase)): if phrase[i] == ' ': listy.insert(0, phrase[i]) dicty[counter] = listy counter += 1 listy = [] if i == len(phrase)-1: listy.append(phrase[i]) dicty[counter] = listy listy = [] if phrase[i] != ' ': listy.append(phrase[i]) string = '' for j in range(0, len(dicty)): k = len(dicty)-j-1 for l in range(0, len(dicty[k])): string += dicty[k][l] continue return string print(word_reverser('Hello everybody, my name is Sally Cowboy and Imma cowboy!'))