Complete the code to calculate the parity bit for error detection.
def calculate_parity(data_bits): parity = sum(data_bits) [1] 2 return parity % 2
The parity bit is calculated by summing the data bits and taking the remainder when divided by 2 using the floor division operator.
Complete the code to detect if there is an error using parity check.
def detect_error(received_bits): parity = sum(received_bits) [1] 2 return parity != 0
The parity check uses modulo 2 to determine if the sum of bits is odd, indicating an error.
Fix the error in the code to correctly calculate the Hamming distance between two bit strings.
def hamming_distance(bits1, bits2): return sum(b1 != b2 for b1, b2 in zip(bits1, [1]))
The Hamming distance compares corresponding bits from both bit strings, so we must zip bits1 and bits2.
Fill both blanks to create a dictionary comprehension that maps each bit string to its parity bit.
parity_dict = [1]: sum([2]) % 2 for [1] in bit_strings}
We iterate over each bit string named 'bits' and calculate parity by summing bits in 'bit'.
Fill all three blanks to filter bit strings with parity bit 1 and create a list of their indices.
indices = [i for i, bits in enumerate([1]) if sum(bits) [2] 2 == [3] 1]
We enumerate over bit_strings, check if sum of bits modulo 2 equals 1 to find odd parity, then collect indices.
