generate functionTue, 28 Feb 2023

Using Numpy, write an array coding function (Run-Length Encoding). The function of repetition of elements compresses the function into one element and considers the number of repetitions. The function returns a motorcade of two Numpy veins of the same length. The first contains elements, and the second - how many times they need to be repeated. Example: Encode (np.array ([1, 2, 2, 3, 3, 1, 1, 5, 5, 2, 3, 3, 3])) = (np.array ([1, 2, 3, 1, 1, 1, 1, 1, 5, 2, 3]), np.array ([1, 2, 2, 2, 2, 1, 2]))

def encode(arr): unique, count = np.unique(arr, return_counts=True) return unique, count encode(np.array([1, 2, 2, 3, 3, 1, 1, 5, 5, 2, 3, 3, 3]))

Questions about programming?Chat with your personal AI assistant