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.

import numpy as np def encoding(a): a = np.array(a) i = 0 b = [] c = [] for j in range(len(a)): if a[j] == a[i]: c.append(a[i]) else: b.append(len(c)) c = [] i = j b.append(len(c)) return np.array([a[:len(a)-1], b])

Questions about programming?Chat with your personal AI assistant