<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def transform(self, X, y, sample_weight=None):
        '''
        Transforms the time series data with run length encoding of the target variable
        Note this transformation changes the number of samples in the data
        If sample_weight is provided, it is transformed to align to the new target encoding


        Parameters
        ----------
        X : array-like, shape [n_series, ...]
           Time series data and (optionally) contextual data
        y : array-like shape [n_series, ...]
            target variable encoded as a time series
        sample_weight : array-like shape [n_series], default = None
            sample weights

        Returns
        -------
        Xt : array-like, shape [n_rle_series, ]
            transformed time series data
        yt : array-like, shape [n_rle_series]
            target values for each series
        sample_weight_new : array-like shape [n_rle_series]
            sample weights
        '''
        check_ts_data_with_ts_target(X, y)

        Xt, Xc = get_ts_data_parts(X)
        N = len(Xt)  # number of time series

        # transformed data
        yt = []
        Xtt = []
        swt = sample_weight
        Nt = []

        for i in range(N):
            Xi, yi = self._transform(Xt[i], y[i])
            yt+=yi
            Xtt+=Xi
            Nt.append(len(yi)) # number of contiguous class instances

        if Xc is not None:
            Xct = expand_variables_to_segments(Xc, Nt)
            Xtt = TS_Data(Xtt, Xct)

        if sample_weight is not None:
            swt = expand_variables_to_segments(sample_weight, Nt)

        return Xtt, yt, swt