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 run_length_encoding(array): repeats = [] current = None counter = 0 for element in array: if element == current: counter += 1 else: if current is not None: repeats.append((current, counter)) current = element counter = 1 repeats.append((current, counter)) values, counts = zip(*repeats) return np.array(values), np.array(counts) run_length_encoding([1, 1, 2, 2, 2, 5])

Questions about programming?Chat with your personal AI assistant