by bastiaanus · 13y ago · 0 comments
def reverse(str):
left_ptr = 0
right_ptr = len(str) - 1
middle = len(str) / 2
while left_ptr <= middle:
# swap
temp = str[left_ptr]
str[left_ptr] = str[right_ptr]
str[right_ptr]= temp
wouldn't this create an infinite loop? You're never incrementing / decrementing left_ptr or right_ptr.str[0] = ... will trigger an exception
"Double the length of the old string and then cut it in half" isn't against the rules, right?
def reverse(str):
return str[::-1]
Am I missing the point ? def reverse(string='abc123'):
for pos in xrange(-len(string)+1, 1):
string += string[abs(pos)]
return string[len(string)/2:]
Fair?