• Input: “Here goes what you want in life”
  • Output: “life in want you what goes Here”
  • 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 = words[::-1] # OR = reversed(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_words = words[::-1]

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

print(reversed_text)

 


 

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

words = text.split()

reversed_words = []
for reversed_index in range(len(words)-1, -1, -1):
    reversed_words.append(words[reversed_index])

reversed_text = reversed_words[0]
for index in range(1, len(reversed_words)):
    reversed_text += " " + reversed_words[index]

print(reversed_text)

 


 

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

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

print(reversed_text)

 


 

  • Interviewer: Couldn’t figure out any other rewrites. Will ask another question.
  • Me: 😳