Last active
August 25, 2016 21:17
-
-
Save alexminnaar/8f9ed5eb61dcef22c35eefcf2d7022b1 to your computer and use it in GitHub Desktop.
Balanced Interleaving Algorithm
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
import random | |
def a_start_check(): | |
if random.random() > 0.5: | |
print "A first" | |
return True | |
else: | |
print "B first" | |
return False | |
def combine(r_a, r_b): | |
k_a = 0 | |
k_b = 0 | |
combined_ranking = [] | |
a_starts = a_start_check() | |
while (k_a <= len(r_a) - 1) and (k_b <= len(r_b) - 1): | |
if (k_a < k_b) or (k_a == k_b and a_starts): | |
if r_a[k_a] not in combined_ranking: | |
combined_ranking += [r_a[k_a]] | |
k_a += 1 | |
else: | |
if r_b[k_b] not in combined_ranking: | |
combined_ranking += [r_b[k_b]] | |
k_b += 1 | |
print k_a, k_b | |
return combined_ranking |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment