Last active
December 26, 2015 06:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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