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!'))

def word_reverser(phrase):
  phrase = phrase.split()
  newPhrase = phrase[::-1]
  return " ".join(newPhrase)

print(word_reverser('Codecademy rules'))

I have decided to do everything more manually, without using many methods

def word_reverser(phrase): words = list() word = "" for index, let in enumerate(phrase): if let == " " or index == len(phrase): words.insert(0, word) word = "" else: word = word + let if index == len(phrase)-1: words.insert(0, word) return " ".join(words) print(word_reverser('Codecademy rules'))
def word_reverser(phrase): phraseList = phrase.split(); reverseList = [] for x in range(len(phraseList)): reverseList.insert(-(len(phraseList)), phraseList [x]) return " ".join(reverseList) print(word_reverser('Codecademy rules and I am very happy'))

Heya,
thats my solution:

def word_reverser(phrase): # Write your code here revPhrase = '' phraseList = phrase.split() i = 0 for i in range(len(phraseList)): i = i + 1 if i == len(phraseList): revPhrase = revPhrase + phraseList[-i] else: revPhrase = revPhrase + phraseList[-i] + ' ' return revPhrase print(word_reverser('Codecademy rules')) print(word_reverser("May the Fourth be with you"))
  1. create a variable for the reversed phrase
  2. split the provided phrase in a list
  3. set i to 0
  4. iterate through the list of words and but them together in the revPhrase variable beginning with the last word in the list

If the word from the list is the first(last for our reversed phrase) I don’t add a space behind.

I’m just starting with Python, so it isn’t the cleanest code you will see here :smiley:

Cheers

def word_reverser(phrase):
old_sentence = phrase.split() # to separate words
new_sentence = “”
for word in old_sentence:
new_sentence = word+’ '+new_sentence # joining words again in reverse order
return new_sentence[:-1] # this is to eliminate the offending space at the end

print(word_reverser(“Call me Ishmael”))