import numpy as np







def     A__	(       __A ,			__A="utf-8" ,			__A="surrogatepass"						):







    '''simple docstring'''
    _lowerCamelCase  :		str			    =    bin(int.from_bytes(text.encode(__A ,			__A						) ,			"""big"""						)						)[2:]
    return bits.zfill(8 * ((len(__A						) + 7) // 8)						)







def     A__	(       __A ,			__A="utf-8" ,			__A="surrogatepass"						):







    '''simple docstring'''
    _lowerCamelCase  :		Optional[Any]			    =    int(__A ,			2						)
    return n.to_bytes((n.bit_length() + 7) // 8 ,			"""big"""						).decode(__A ,			__A						) or "\0"







def     A__	(       __A ,			__A						):







    '''simple docstring'''
    if size_par + len(__A						) <= 2**size_par - (len(__A						) - 1):
        raise ValueError("""size of parity don't match with size of data"""						)

    _lowerCamelCase  :		List[Any]			    =    []
    _lowerCamelCase  :		List[Any]			    =    []
    _lowerCamelCase  :		Union[str, Any]			    =    [bin(__A						)[2:] for x in range(1 ,			size_par + len(__A						) + 1						)]

    # sorted information data for the size of the output data
    _lowerCamelCase  :		str			    =    []
    # data position template + parity
    _lowerCamelCase  :		List[str]			    =    []
    # parity bit counter
    _lowerCamelCase  :		Union[str, Any]			    =    0
    # counter position of data bits
    _lowerCamelCase  :		Optional[int]			    =    0

    for x in range(1 ,			size_par + len(__A						) + 1						):
        # Performs a template of bit positions - who should be given,
        # and who should be parity
        if qtd_bp < size_par:
            if (np.log(__A						) / np.log(2						)).is_integer():
                data_out_gab.append("""P"""						)
                _lowerCamelCase  :		str			    =    qtd_bp + 1
            else:
                data_out_gab.append("""D"""						)
        else:
            data_out_gab.append("""D"""						)

        # Sorts the data to the new output size
        if data_out_gab[-1] == "D":
            data_ord.append(data[cont_data]						)
            cont_data += 1
        else:
            data_ord.append(__A						)

    # Calculates parity
    _lowerCamelCase  :		Union[str, Any]			    =    0  # parity bit counter
    for bp in range(1 ,			size_par + 1						):
        # Bit counter one for a given parity
        _lowerCamelCase  :		Union[str, Any]			    =    0
        # counter to control the loop reading
        _lowerCamelCase  :		Union[str, Any]			    =    0
        for x in data_ord:
            if x is not None:
                try:
                    _lowerCamelCase  :		List[Any]			    =    (bin_pos[cont_loop])[-1 * (bp)]
                except IndexError:
                    _lowerCamelCase  :		Dict			    =    """0"""
                if aux == "1" and x == "1":
                    cont_bo += 1
            cont_loop += 1
        parity.append(cont_bo % 2						)

        qtd_bp += 1

    # Mount the message
    _lowerCamelCase  :		List[Any]			    =    0  # parity bit counter
    for x in range(0 ,			size_par + len(__A						)						):
        if data_ord[x] is None:
            data_out.append(str(parity[cont_bp]						)						)
            cont_bp += 1
        else:
            data_out.append(data_ord[x]						)

    return data_out







def     A__	(       __A ,			__A						):







    '''simple docstring'''
    _lowerCamelCase  :		List[Any]			    =    []
    # Parity bit counter
    _lowerCamelCase  :		Optional[Any]			    =    0
    # Counter p data bit reading
    _lowerCamelCase  :		str			    =    0
    # list of parity received
    _lowerCamelCase  :		str			    =    []
    _lowerCamelCase  :		Dict			    =    []

    for x in range(1 ,			len(__A						) + 1						):
        # Performs a template of bit positions - who should be given,
        #  and who should be parity
        if qtd_bp < size_par and (np.log(__A						) / np.log(2						)).is_integer():
            data_out_gab.append("""P"""						)
            _lowerCamelCase  :		str			    =    qtd_bp + 1
        else:
            data_out_gab.append("""D"""						)

        # Sorts the data to the new output size
        if data_out_gab[-1] == "D":
            data_output.append(data[cont_data]						)
        else:
            parity_received.append(data[cont_data]						)
        cont_data += 1

    # -----------calculates the parity with the data
    _lowerCamelCase  :		str			    =    []
    _lowerCamelCase  :		Union[str, Any]			    =    []
    _lowerCamelCase  :		Union[str, Any]			    =    [bin(__A						)[2:] for x in range(1 ,			size_par + len(__A						) + 1						)]

    #  sorted information data for the size of the output data
    _lowerCamelCase  :		Union[str, Any]			    =    []
    # Data position feedback + parity
    _lowerCamelCase  :		Optional[int]			    =    []
    # Parity bit counter
    _lowerCamelCase  :		int			    =    0
    # Counter p data bit reading
    _lowerCamelCase  :		Dict			    =    0

    for x in range(1 ,			size_par + len(__A						) + 1						):
        # Performs a template position of bits - who should be given,
        # and who should be parity
        if qtd_bp < size_par and (np.log(__A						) / np.log(2						)).is_integer():
            data_out_gab.append("""P"""						)
            _lowerCamelCase  :		Any			    =    qtd_bp + 1
        else:
            data_out_gab.append("""D"""						)

        # Sorts the data to the new output size
        if data_out_gab[-1] == "D":
            data_ord.append(data_output[cont_data]						)
            cont_data += 1
        else:
            data_ord.append(__A						)

    # Calculates parity
    _lowerCamelCase  :		List[str]			    =    0  # parity bit counter
    for bp in range(1 ,			size_par + 1						):
        # Bit counter one for a certain parity
        _lowerCamelCase  :		Union[str, Any]			    =    0
        # Counter to control loop reading
        _lowerCamelCase  :		List[str]			    =    0
        for x in data_ord:
            if x is not None:
                try:
                    _lowerCamelCase  :		List[str]			    =    (bin_pos[cont_loop])[-1 * (bp)]
                except IndexError:
                    _lowerCamelCase  :		List[Any]			    =    """0"""
                if aux == "1" and x == "1":
                    cont_bo += 1
            cont_loop += 1
        parity.append(str(cont_bo % 2						)						)

        qtd_bp += 1

    # Mount the message
    _lowerCamelCase  :		Dict			    =    0  # Parity bit counter
    for x in range(0 ,			size_par + len(__A						)						):
        if data_ord[x] is None:
            data_out.append(str(parity[cont_bp]						)						)
            cont_bp += 1
        else:
            data_out.append(data_ord[x]						)

    _lowerCamelCase  :		int			    =    parity_received == parity
    return data_output, ack





from __future__ import annotations







def     A__	(       __A ,			__A						):







    '''simple docstring'''
    _lowerCamelCase  :		List[str]			    =    [None] * (len(__A						) + len(__A						))

    _lowerCamelCase  :		Optional[int]			    =    0  # pointer to current index for left Half
    _lowerCamelCase  :		List[str]			    =    0  # pointer to current index for the right Half
    _lowerCamelCase  :		List[str]			    =    0  # pointer to current index for the sorted array Half

    while pointera < len(__A						) and pointera < len(__A						):
        if left_half[pointera] < right_half[pointera]:
            _lowerCamelCase  :		Optional[Any]			    =    left_half[pointera]
            pointera += 1
            index += 1
        else:
            _lowerCamelCase  :		List[Any]			    =    right_half[pointera]
            pointera += 1
            index += 1
    while pointera < len(__A						):
        _lowerCamelCase  :		int			    =    left_half[pointera]
        pointera += 1
        index += 1

    while pointera < len(__A						):
        _lowerCamelCase  :		Optional[Any]			    =    right_half[pointera]
        pointera += 1
        index += 1

    return sorted_array







def     A__	(       __A						):







    '''simple docstring'''
    if len(__A						) <= 1:
        return array
    # the actual formula to calculate the middle element = left + (right - left) // 2
    # this avoids integer overflow in case of large N
    _lowerCamelCase  :		int			    =    0 + (len(__A						) - 0) // 2

    # Split the array into halves till the array length becomes equal to One
    # merge the arrays of single length returned by mergeSort function and
    # pass them into the merge arrays function which merges the array
    _lowerCamelCase  :		List[str]			    =    array[:middle]
    _lowerCamelCase  :		Optional[int]			    =    array[middle:]

    return merge(merge_sort(__A						) ,			merge_sort(__A						)						)


if __name__ == "__main__":
    import doctest

    doctest.testmod()




