{"record":{"id":"43af3ce45688bad4","repo":"TheAlgorithms/Python","slug":"size-of-parity-don-t-match-with-size-of-data","errorCode":null,"errorMessage":"size of parity don't match with size of data","messagePattern":"size of parity don't match with size of data","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"hashes/hamming_code.py","lineNumber":86,"sourceCode":"\n\n# Functions of hamming code-------------------------------------------\ndef emitter_converter(size_par, data):\n    \"\"\"\n    :param size_par: how many parity bits the message must have\n    :param data:  information bits\n    :return: message to be transmitted by unreliable medium\n            - bits of information merged with parity bits\n\n    >>> emitter_converter(4, \"101010111111\")\n    ['1', '1', '1', '1', '0', '1', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1']\n    >>> emitter_converter(5, \"101010111111\")\n    Traceback (most recent call last):\n        ...\n    ValueError: size of parity don't match with size of data\n    \"\"\"\n    if size_par + len(data) <= 2**size_par - (len(data) - 1):\n        raise ValueError(\"size of parity don't match with size of data\")\n\n    data_out = []\n    parity = []\n    bin_pos = [bin(x)[2:] for x in range(1, size_par + len(data) + 1)]\n\n    # sorted information data for the size of the output data\n    data_ord = []\n    # data position template + parity\n    data_out_gab = []\n    # parity bit counter\n    qtd_bp = 0\n    # counter position of data bits\n    cont_data = 0\n\n    for x in range(1, size_par + len(data) + 1):\n        # Performs a template of bit positions - who should be given,\n        # and who should be parity\n        if qtd_bp < size_par:","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/hashes/hamming_code.py#L68-L104","documentation":"Thrown by emitter_converter() in the Hamming code module when the number of parity bits (size_par) does not fit the data length. The guard raises when size_par + len(data) <= 2**size_par - (len(data) - 1), i.e. when the parity count is inconsistent with the payload: for a 12-bit data string, size_par=4 works (16 total bits) but size_par=5 raises because the parity scheme would produce a mismatched codeword layout.","triggerScenarios":"Calling emitter_converter(5, \"101010111111\") raises; emitter_converter(4, \"101010111111\") succeeds. Also triggered when data length changes (shorter/longer bit string) while size_par is kept fixed from a previous configuration.","commonSituations":"Hardcoding the parity size from an example while feeding different payload lengths; porting code where the data word size changed; generating test vectors with arbitrary (size_par, data) pairs.","solutions":["Match size_par to the data length: for 12 data bits use size_par=4 (2**4 = 16 >= 4 + 12).","Compute size_par programmatically: smallest p such that 2**p >= p + len(data) + 1, then verify against the guard's condition before calling.","Keep (size_par, data length) as one configuration unit so they are never changed independently."],"exampleFix":"# before\nemitter_converter(5, \"101010111111\")  # ValueError\n\n# after\nimport math\ndef parity_size(n_data: int) -> int:\n    return next(p for p in range(1, 17) if 2**p >= p + n_data + 1)\nemitter_converter(parity_size(len(data)), data)","handlingStrategy":"validation","validationCode":"def parity_size(n_data: int) -> int:\n    return next(p for p in range(1, 17) if 2**p >= p + n_data + 1)\n\nsize_par = parity_size(len(data))","typeGuard":"def is_valid_hamming_config(size_par: int, data: str) -> bool:\n    return size_par > 0 and not (size_par + len(data) <= 2**size_par - (len(data) - 1))","tryCatchPattern":"try:\n    codeword = emitter_converter(size_par, data)\nexcept ValueError as e:\n    raise ValueError(\n        f\"parity size {size_par} invalid for {len(data)}-bit payload\"\n    ) from e","preventionTips":["Never hardcode size_par when data length varies; compute it.","Ship (size_par, data_length) as one config pair.","Round-trip test emitter/receiver with the same size_par."],"tags":["hamming-code","error-correction","validation","bit-manipulation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}