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”))

The list constructor isn’t needed since split() returns a list.

>>> def word_reverser(phrase):
...     return ' '.join(phrase.split()[::-1])
... 
>>> word_reverser("May the Fourth be with you")
'you with be Fourth the May'
>>> 
1 Like

Seems like I solved this in a way more complicated way than necessary but I am happy to see other ways of solving this. Good job everyone!

def word_reverser(string): reversed_string = "" first_list = [] for word in string.split(" "): first_list.append(word) first_len = len(first_list) counter = 0 while counter != first_len: last_ = first_list.pop() reversed_string += last_ reversed_string += " " counter += 1 reversed_string = reversed_string.rstrip() return(reversed_string) print(word_reverser('Codecademy rules'))
1 Like

here is my solution feel free to give feedback

def word_reverser(phrase):
  # Write your code here
  rev = ""
  words = []
  words.append(phrase.split())
  for n in range(1,len(words[0])+1):
    rev = rev + words[0][-n] +" "
  return rev.strip()

print(word_reverser('Codecademy rules'))
def word_reverser(phrase): # Write your code here return " ".join(phrase.split()[::-1]) print(word_reverser('Codecademy rules'))

def word_reverser(phrase):
return " ".join(word for word in phrase.split()[::-1])
print(word_reverser(‘Codeacademy rules’))

def word_reverser(phrase): txt = phrase t = "" x = txt.split() for item in x: t = item +" " + t p = t.strip(" ") return p print(word_reverser("May the Fourth be with you")) print(word_reverser('Codecademy rules'))
def word_reverser(phrase): # Write your code here l=[] w = phrase.split(' ') l.extend(w) l.reverse() word = ' '.join(l) return word print(word_reverser('Codecademy rules'))
def word_reverser(phrase): list_of_words = phrase.split() reversed_words = [] for i in range (len(list_of_words)): reversed_words.append(list_of_words.pop()) reversed_phrase = " ".join(reversed_words) return(reversed_phrase) print(word_reverser('Codecademy rules'))