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(a): c = [] index = 0 a_len = len(a) while index < a_len: c.append(a[index]) b = 1 while index + 1 < a_len and a[index] == a[index + 1]: index += 1 b += 1 c.append(b) index += 1 c = np.array(c) return c[0::2], c[1::2] encode(np.array([1,2,2,3,3,1,1,5,5,2,3,3,3,2,2]))

Questions about programming?Chat with your personal AI assistant