Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-293

Nearest Codeword Decode

Problem

Decode a received binary word by maximum-likelihood nearest-neighbor decoding. Return the index of the codeword at minimum Hamming distance from received, breaking ties toward the smallest index. Return -1 when the code list is empty.

Hint

Strictly less than the best distance keeps the earliest index on ties.

Test cases

case 1
in: [[1,0,1],[[0,0,0],[1,0,0],[1,1,1]]]
expected: 1
case 2
in: [[0,0,1],[[1,1,0],[0,0,0]]]
expected: 1
case 3
in: [[1,1],[]]
expected: -1
case 4
in: [[0,0,0],[[0,0,1],[0,0,0]]]
expected: 1
Starter codePython
def nearest_codeword(received, code):
    # Your code here
    pass

4 test cases · run your code against them in the editor