multi point crossover with numpy

  arrays, numpy, python

I try to understand how to write multi point crossover with numpy. For example I have two array like

a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = np.array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

And K points

np.array([1, 5, 8])

Ansver shoul will be like

0 1 7 6 5 5 6 7 8 0
9 8 2 3 4 4 3 2 1 9

I figured out how it will be for two points

def two_point(a, b, point_1, point_2):
   for i in range(point_1+1, point_2):
        a[i], b[i] = b[i], a[i]
    return a, b

But I have trouble with k points

Source: Python Questions

LEAVE A COMMENT