counting_bits

 1# @leet start
 2class Solution:
 3    def countBits(self, n: int) -> list[int]:
 4        """
 5        This question asks for the count of all bits in a range from 0 <= n.
 6        To count the bits using an intrinsic, we can use integer.bit_count
 7        but I manually wrote an implementation and used it.
 8        The `n &= n - 1` trick counts the bits by always removing the last
 9        set bit.
10        """
11
12        def bit_count(n: int) -> int:
13            count = 0
14            while n:
15                count += 1
16                n &= n - 1
17            return count
18
19        return [bit_count(x) for x in range(n + 1)]
20
21
22# @leet end
23q = Solution().countBits
24
25
26def test():
27    assert q(2) == [0, 1, 1]
28    assert q(5) == [0, 1, 1, 2, 1, 2]
class Solution:
 3class Solution:
 4    def countBits(self, n: int) -> list[int]:
 5        """
 6        This question asks for the count of all bits in a range from 0 <= n.
 7        To count the bits using an intrinsic, we can use integer.bit_count
 8        but I manually wrote an implementation and used it.
 9        The `n &= n - 1` trick counts the bits by always removing the last
10        set bit.
11        """
12
13        def bit_count(n: int) -> int:
14            count = 0
15            while n:
16                count += 1
17                n &= n - 1
18            return count
19
20        return [bit_count(x) for x in range(n + 1)]
def countBits(self, n: int) -> list[int]:
 4    def countBits(self, n: int) -> list[int]:
 5        """
 6        This question asks for the count of all bits in a range from 0 <= n.
 7        To count the bits using an intrinsic, we can use integer.bit_count
 8        but I manually wrote an implementation and used it.
 9        The `n &= n - 1` trick counts the bits by always removing the last
10        set bit.
11        """
12
13        def bit_count(n: int) -> int:
14            count = 0
15            while n:
16                count += 1
17                n &= n - 1
18            return count
19
20        return [bit_count(x) for x in range(n + 1)]

This question asks for the count of all bits in a range from 0 <= n. To count the bits using an intrinsic, we can use integer.bit_count but I manually wrote an implementation and used it. The n &= n - 1 trick counts the bits by always removing the last set bit.

def q(n: int) -> list[int]:
 4    def countBits(self, n: int) -> list[int]:
 5        """
 6        This question asks for the count of all bits in a range from 0 <= n.
 7        To count the bits using an intrinsic, we can use integer.bit_count
 8        but I manually wrote an implementation and used it.
 9        The `n &= n - 1` trick counts the bits by always removing the last
10        set bit.
11        """
12
13        def bit_count(n: int) -> int:
14            count = 0
15            while n:
16                count += 1
17                n &= n - 1
18            return count
19
20        return [bit_count(x) for x in range(n + 1)]

This question asks for the count of all bits in a range from 0 <= n. To count the bits using an intrinsic, we can use integer.bit_count but I manually wrote an implementation and used it. The n &= n - 1 trick counts the bits by always removing the last set bit.

def test():
27def test():
28    assert q(2) == [0, 1, 1]
29    assert q(5) == [0, 1, 1, 2, 1, 2]