Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-318

Hamming Weight of a Codeword

Problem

Return the Hamming weight of a bit list, which is the number of entries equal to 1. The input is a list of 0/1 integers. Return 0 for an empty list.

Hint

Iterate once and count the bits that are equal to 1.

Test cases

case 1
in: [[1,0,1,1]]
expected: 3
case 2
in: [[]]
expected: 0
case 3
in: [[0,0]]
expected: 0
case 4
in: [[1,1]]
expected: 2
case 5
in: [[0,1,0,1,0]]
expected: 2
Starter codePython
def hamming_weight(bits):
    # Your code here
    pass

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