Created
November 10, 2013 18:05
-
-
Save prat0318/7401641 to your computer and use it in GitHub Desktop.
rotate and find_min
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
def swap(a, b): | |
temp = a | |
a = b | |
b = temp | |
return (a,b) | |
def rotate(arr, by): | |
if len(arr)%by == 0: | |
for i in range(by): | |
start, pos = arr[i], i+by | |
for j in range(len(arr)/by): | |
arr[pos%len(arr)], start = swap(arr[pos%len(arr)], start) | |
pos += by | |
else: | |
start, pos = arr[0], by | |
for i in range(len(arr)): | |
arr[pos%len(arr)], start = swap(arr[pos%len(arr)], start) | |
pos += by | |
return arr | |
def find_min(arr, start, end): | |
if(start >= end): | |
return arr[start] | |
mid = (start+end)/2 | |
if arr[mid] <= arr[end]: | |
if arr[mid-1] > arr[mid]: | |
return arr[mid] | |
return find_min(arr, start, mid-1) | |
else: | |
return find_min(arr, mid+1, end) | |
#errors found were: take care of base recursion | |
#errors found were: add "return" recursion_method | |
#errors found were: no. of iterations were one less, | |
#errors found were: using % instead of / | |
print rotate([1,2,3,4],1) #output: [4,1,2,3] | |
print find_min(rotate([1,2,3,4],1),0,3) #output: 1 | |
print rotate([1,2,3,4],3) #output: [2,3,4,1] | |
print find_min(rotate([1,2,3,4],3),0,3) #output: 1 | |
print rotate([1,2,3,4],2) #output: [3,4,1,2] | |
print find_min(rotate([1,2,3,4],2),0,3) #output: 1 | |
print find_min([1,2],0,1) | |
print find_min([1,1],0,1) | |
print find_min([1],0,0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment