Skip to content

Instantly share code, notes, and snippets.

@alexminnaar
Last active August 25, 2016 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexminnaar/8f9ed5eb61dcef22c35eefcf2d7022b1 to your computer and use it in GitHub Desktop.
Save alexminnaar/8f9ed5eb61dcef22c35eefcf2d7022b1 to your computer and use it in GitHub Desktop.
Balanced Interleaving Algorithm
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