Created
October 22, 2013 09:56
-
-
Save johnpaulhayes/7098027 to your computer and use it in GitHub Desktop.
Query two arrays for the k-th largest
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
a = [1, 5, 10, 15] | |
b = [3, 6, 8, 20] | |
def query_arrays(a, b, k): | |
i,j=0,0 | |
merged_array = [] | |
while i <= len(a)-1 and j <= len(b)-1: | |
if a[i] < b[j]: | |
merged_array.append(a[i]) | |
i += 1 | |
else: | |
merged_array.append(b[j]) | |
j += 1 | |
print merged_array | |
print merged_array[len(merged_array)-k] | |
query_arrays(a, b, 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment