TheAlgorithms/Python · error · ValueError
size of parity don't match with size of data
Error message
size of parity don't match with size of data
What it means
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.
Source
Thrown at hashes/hamming_code.py:86
# Functions of hamming code-------------------------------------------
def emitter_converter(size_par, data):
"""
:param size_par: how many parity bits the message must have
:param data: information bits
:return: message to be transmitted by unreliable medium
- bits of information merged with parity bits
>>> emitter_converter(4, "101010111111")
['1', '1', '1', '1', '0', '1', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1']
>>> emitter_converter(5, "101010111111")
Traceback (most recent call last):
...
ValueError: size of parity don't match with size of data
"""
if size_par + len(data) <= 2**size_par - (len(data) - 1):
raise ValueError("size of parity don't match with size of data")
data_out = []
parity = []
bin_pos = [bin(x)[2:] for x in range(1, size_par + len(data) + 1)]
# sorted information data for the size of the output data
data_ord = []
# data position template + parity
data_out_gab = []
# parity bit counter
qtd_bp = 0
# counter position of data bits
cont_data = 0
for x in range(1, size_par + len(data) + 1):
# Performs a template of bit positions - who should be given,
# and who should be parity
if qtd_bp < size_par:View on GitHub (pinned to f5988cc097)
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.
Example fix
# before
emitter_converter(5, "101010111111") # ValueError
# after
import math
def parity_size(n_data: int) -> int:
return next(p for p in range(1, 17) if 2**p >= p + n_data + 1)
emitter_converter(parity_size(len(data)), data) Defensive patterns
Strategy: validation
Validate before calling
def parity_size(n_data: int) -> int:
return next(p for p in range(1, 17) if 2**p >= p + n_data + 1)
size_par = parity_size(len(data)) Type guard
def is_valid_hamming_config(size_par: int, data: str) -> bool:
return size_par > 0 and not (size_par + len(data) <= 2**size_par - (len(data) - 1)) Try / catch
try:
codeword = emitter_converter(size_par, data)
except ValueError as e:
raise ValueError(
f"parity size {size_par} invalid for {len(data)}-bit payload"
) from e Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- number must be positive
- The value of input must be non-negative
- Shift must be non-negative
- Input value must be a 'int' type
- the value of both inputs must be positive
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/43af3ce45688bad4.
Report an issue: GitHub.