Given N non-comparable arrays of different sizes, what is the best method to combine them into one output array?
Since the input arrays are non-comparable, a metric is needed to represent how frequently the value on top of the array in question is appended to the output array. Lets call it
weight and let it be an arbitrary value from
Here is an example data set with weights:
numbers = [1, 2, 3, 4, 5] numbers_weight = 3 books = ['The Lord of the Rings', 'The Hitchhikers Guide to the Galaxy', 'Enders Game', '1984'] books_weight = 5 words = ['polar', 'fiendish', 'percussive'] words_weight = 10
A naive solution would be generate a random number and then ask each array if it passes or fails based on its weight. If it passes, append it to the end of the output array. Then generate a new random number and begin again until some maximum limit is reached.
Here is the naive solution acting on the sample data:
def join_arrays(arrays, weights, weight_min, weight_max, output_limit): output =  while True: rnd = random.randint(weight_min, weight_max) for i in range(len(arrays)): if rnd <= weights[i] and len(arrays[i]) > 0: output.append(arrays[i].pop(0)) if len(output) >= output_limit: return output
And here is how the function is called, again using the sample data:
arrays = [numbers, books, words] weights = [numbers_weight, books_weight, words_weight] random.seed(time.time()) output = join_arrays(arrays, weights, 1, 10, 6)
The problem that I have with this type of round-robin style implementation is that if the size of
arrays is vastly larger than
output_limit, those towards the back of
arrays will appear less frequently than their weight would suggest, or not at all, because there will be instances where they simply were not asked if they pass, and thus not appended to
Is there a solution to this problem that is allows for all input arrays inside of
arrays to be asked if they pass or fail, thereby being fair, while still allowing for an
Are the use of weights even appropriate for this type of combining?