Question: Say we want to write a function in Python that takes a string and reverses the order of the words in place. How would you do it?
Answer: In python, C# and Java (and many other languages) strings are immutable so this is not possible to do. But let’s say we use the list representation of the string and want to reverse the order of words in place. The following is the shortest code that I can come up with.
s = list(reversed(si))
l = len(s)
prevspace = 0
for i in range(0, l):
if s[i]==" ":
s[prevspace:i] = reversed(s[prevspace:i])
prevspace = i+1
print reverse(" this is a test")
print reverse("this is another test")
print reverse(" this is the final t est ")