Created
February 13, 2016 04:34
-
-
Save sgallese/a5a9f8e63d05ad9474d9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def rotstring( input_string, rotate_distance ): | |
string_length = len(input_string) | |
print "input: " + input_string | |
print "rotate_distance: " + str(rotate_distance) | |
print "string_length: " + str(string_length) | |
if rotate_distance == 0 or rotate_distance == string_length: | |
print "Rotate distance is 0 or rotate distance = string length, exiting" | |
return | |
p = rotate_distance | |
print "p(rotate distance): " + str(p) | |
i = rotate_distance | |
print "i(rotate distance): " + str(i) | |
j = string_length - p | |
print "j(string length - p(rotate distance)): " + str(j) | |
while i != j: | |
print "while i(" + str(i) + ") != j(" + str(j) + ")" | |
if i > j: | |
print "i > j" | |
input_string = swap(input_string, p-i, p, j) | |
print "swap( p(" + str(p) + ")-i(" + str(i) + "), p(" + str(p) + "), j(" + str(j) + ") )" | |
print "swap result: " + input_string | |
i -= j | |
else: | |
print "i <= j" | |
input_string = swap(input_string, p-i, p+j-i, i) | |
print "swap( p(" + str(p) + ")-i(" + str(i) + "), p(" + str(p) + ")+j(" + str(j) + ")-i(" + str(i) + "), i(" + str(i) + ") )" | |
print "swap result: " + input_string | |
j -= i | |
input_string = swap(input_string, p-i, p, i) | |
print "final swap( p(" + str(p) + ")-i(" + str(i) + "), p(" + str(p) + "), i(" + str(i) + ") )" | |
return input_string | |
def swap(input_string, i, j, k): | |
# exchanges x[i..i+k-1] with x[j..j+k-1] | |
# swap("abcd", 0, 3, 1) -> dbca | |
while k > 0: | |
temp = input_string[i] | |
string_list = list(input_string) | |
string_list[i] = input_string[j] | |
string_list[j] = temp | |
input_string = ''.join(string_list) | |
i += 1 | |
j += 1 | |
k -= 1 | |
return input_string | |
print rotstring( "ab", 1) | |
#print swap("abcd", 0, 3, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment