Skip to content

Instantly share code, notes, and snippets.

@curiouslychase
Last active December 26, 2015 06:59
Show Gist options
  • Save curiouslychase/7112209 to your computer and use it in GitHub Desktop.
Save curiouslychase/7112209 to your computer and use it in GitHub Desktop.
[python response] Here's the solution I came up with for this interview question on interviewcake.com: "Write a function to reverse a string in place. 'In place' means 'without creating a new string in memory.'" http://www.interviewcake.com/question/reverse-string-in-place
import sys
def reverso(str):
i = 0
str_len = len(str) - 1
str = list(str)
half_len = len(str)/2
for x in str:
temp = x
str[i] = str[str_len]
str[str_len] = temp
str_len -= 1
i += 1
if half_len == str_len:
print "".join(str)
reverso(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment