


import numpy as np
def 	_lowerCamelCase  (		A_  :	List[str]							,  A_  :	Tuple="utf-8"							,  A_  :	Any="surrogatepass"      )			->			str:

   '''simple docstring'''






   UpperCamelCase__ :			str 						=bin(int.from_bytes(text.encode(lowerCamelCase__							,  lowerCamelCase__      )							,  "big"      )      )[2:]
   return bits.zfill(8 * ((len(lowerCamelCase__      ) + 7) // 8)      )
def 	_lowerCamelCase  (		A_  :	List[Any]							,  A_  :	str="utf-8"							,  A_  :	Tuple="surrogatepass"      )			->			Any:

   '''simple docstring'''






   UpperCamelCase__ :			Optional[Any] 						=int(lowerCamelCase__							,  2      )
   return n.to_bytes((n.bit_length() + 7) // 8							,  "big"      ).decode(lowerCamelCase__							,  lowerCamelCase__      ) or "\0"
def 	_lowerCamelCase  (		A_  :	Union[str, Any]							,  A_  :	Optional[Any]      )			->			List[str]:

   '''simple docstring'''






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

   UpperCamelCase__ :			str 						=[]
   UpperCamelCase__ :			List[str] 						=[]
   UpperCamelCase__ :			Dict 						=[bin(lowerCamelCase__      )[2:] for x in range(1							,  size_par + len(lowerCamelCase__      ) + 1      )]

   # sorted information data for the size of the output data
   UpperCamelCase__ :			List[str] 						=[]
   # data position template + parity
   UpperCamelCase__ :			List[str] 						=[]
   # parity bit counter
   UpperCamelCase__ :			List[Any] 						=0
   # counter position of data bits
   UpperCamelCase__ :			Tuple 						=0

   for x in range(1							,  size_par + len(lowerCamelCase__      ) + 1      ):
      # Performs a template of bit positions - who should be given,
      # and who should be parity
      if qtd_bp < size_par:
         if (np.log(lowerCamelCase__      ) / np.log(2      )).is_integer():
            data_out_gab.append("P"      )
            UpperCamelCase__ :			Union[str, Any] 						=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(lowerCamelCase__      )

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

      qtd_bp += 1

   # Mount the message
   UpperCamelCase__ :			Union[str, Any] 						=0  # parity bit counter
   for x in range(0							,  size_par + len(lowerCamelCase__      )      ):
      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 	_lowerCamelCase  (		A_  :	Dict							,  A_  :	Union[str, Any]      )			->			Dict:

   '''simple docstring'''






   UpperCamelCase__ :			Optional[Any] 						=[]
   # Parity bit counter
   UpperCamelCase__ :			int 						=0
   # Counter p data bit reading
   UpperCamelCase__ :			str 						=0
   # list of parity received
   UpperCamelCase__ :			Any 						=[]
   UpperCamelCase__ :			List[str] 						=[]

   for x in range(1							,  len(lowerCamelCase__      ) + 1      ):
      # Performs a template of bit positions - who should be given,
      #  and who should be parity
      if qtd_bp < size_par and (np.log(lowerCamelCase__      ) / np.log(2      )).is_integer():
         data_out_gab.append("P"      )
         UpperCamelCase__ :			int 						=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
   UpperCamelCase__ :			Union[str, Any] 						=[]
   UpperCamelCase__ :			Dict 						=[]
   UpperCamelCase__ :			List[Any] 						=[bin(lowerCamelCase__      )[2:] for x in range(1							,  size_par + len(lowerCamelCase__      ) + 1      )]

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

   for x in range(1							,  size_par + len(lowerCamelCase__      ) + 1      ):
      # Performs a template position of bits - who should be given,
      # and who should be parity
      if qtd_bp < size_par and (np.log(lowerCamelCase__      ) / np.log(2      )).is_integer():
         data_out_gab.append("P"      )
         UpperCamelCase__ :			List[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(lowerCamelCase__      )

          # Calculates parity
   UpperCamelCase__ :			Union[str, Any] 						=0  # parity bit counter
   for bp in range(1							,  size_par + 1      ):
      # Bit counter one for a certain parity
      UpperCamelCase__ :			Dict 						=0
      # Counter to control loop reading
      UpperCamelCase__ :			Optional[Any] 						=0
      for x in data_ord:
         if x is not None:
            try:
               UpperCamelCase__ :			List[str] 						=(bin_pos[cont_loop])[-1 * (bp)]
            except IndexError:
               UpperCamelCase__ :			Tuple 						='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
   UpperCamelCase__ :			List[str] 						=0  # Parity bit counter
   for x in range(0							,  size_par + len(lowerCamelCase__      )      ):
      if data_ord[x] is None:
         data_out.append(str(parity[cont_bp]      )      )
         cont_bp += 1
      else:
         data_out.append(data_ord[x]      )

   UpperCamelCase__ :			Tuple 						=parity_received == parity
   return data_output, ack









def       _a   (							lowerCamelCase__				)  ->      int:
 return int(lowerCamelCase__				) if x - int(lowerCamelCase__				) >= 0 else int(lowerCamelCase__				) - 1


if __name__ == "__main__":
       import doctest

       doctest.testmod()
