• Input: “Here goes what you want in life”
  • Output: “ereH seog tahw uoy tnaw ni efil”
  • Assumption: Input contains at least one word.

 


 

  • Quick code using all buit in functions.
text = "Here goes what you want in life"

words = text.split()
reversed_words = [word[::-1] for word in words]
reversed_text = " ".join(reversed_words)

print(reversed_text)

 


 

  • Interviewer: Rewrite without join().
text = "Here goes what you want in life"

words = text.split()

reversed_text = words[0][::-1]
for word in words[1:]:
    reversed_text += " " + word[::-1]

print(reversed_text)

 


 

  • Interviewer: Rewrite without slicing.
text = "Here goes what you want in life"

reversed_text = ""
next_word = ""
for letter in text:
    if letter == " ":
        reversed_text += next_word + " "
        next_word = ""
    else:
        next_word = letter + next_word
reversed_text += next_word

print(reversed_text)