I have n ordered lists of unequal size (where I do not know beforehand how many lists there are going to be). I need to find the minimum average distance between one element in each list.
For example given n=3 for three lists:
a = [14, 22, 36, 48]
b = [14, 23, 30, 72]
c = [1, 18, 24]
The output should be (22,23,24) because:
mean(abs(22-23), abs(23-24), abs(22-24)) = 1.33333
which is the smallest among all the points in the example above.
I tried to implement it in Python as following
def aligner(aoa):
'''
read arrays of arrays of peaks and return closest peaks
'''
#one of arrays is empty
if not [y for x in aoa for y in x]:
return None
# there is the same nr in all array no need to do anything
candidate = set.intersection(*map(set, aoa))
if candidate:
# returns intersect
return [max(list(candidate))] * len(aoa)
else:
#tried cartesian product via bumpy malloc err
pass
My doubt is now regarding the implementation of the other part. I thought about using the cartesian product for generating all combination but is going into memory issues. My guesses would be do generate all the combination somehow (maybe itertools??) and loop through all of those but I don't know if there s any algorithm that tackles this problem that I could use.
I do not need code but only hints whether there is any efficient way for solving this or the brute force with n for loops on the permutated lists is the only one
EDIT
Regarding the size of the problem the nr of list is maximum 100 (fixed) while the nr of elements can be vary but I would say that the presented example with 4 or 5 points per list is a realistic scenario.
All points are non-negative.
Tried the proposed itertools solution but of course not memory issues but has been running since hours and it's stuck on the 3rd element.