# in-place def reverseWords(self, s): # reverse the whole list self.reverse(s, 0, len(s)-1) r = 0 while r < len(s): l = r while r < len(s) and s[r] != " ": r += 1 # reverse each sublist self.reverse(s, l, r-1) r += 1 def reverse(self, s, l, r): while l < r: s[l], s[r] = s[r], s[l] l +=1 ; r -= 1
Question: I was implementing the reverse function in Python, but it says 'str' object does not support item assignment. Does it matter to this question? Thanks!
If s is a string in Python, you should not do like "s[i] = ...", bacause string is immutable in Python. Here you can find more explanation (http://stackoverflow.com/questions/9453820/alternative-to-python-string-item-assignment), hope it helps you.