물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Range Bit Counting

BEstyle 2022. 9. 30. 18:45

Task

You are given two numbers a and b where 0 ≤ a ≤ b. Imagine you construct an array of all the integers from a to b inclusive. You need to count the number of 1s in the binary representations of all the numbers in the array.

Example

For a = 2 and b = 7, the output should be 11

Given a = 2 and b = 7 the array is: [2, 3, 4, 5, 6, 7]. Converting the numbers to binary, we get [10, 11, 100, 101, 110, 111], which contains 1 + 2 + 1 + 2 + 2 + 3 = 11 1s.

Input/Output

  • [input] integer a

Constraints: 0 ≤ a ≤ b.

  • [input] integer b

Constraints: a ≤ b ≤ 100.

  • [output] an integer

def range_bit_count(a, b):
    ans=0
    for i in range(a,b+1):
        ans+=format(i,'b').count('1')
    return ans

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Remove the minimum  (0) 2022.09.30
Count the divisors of a number  (0) 2022.09.30
Ascend, Descend, Repeat?  (0) 2022.09.29
Find the missing letter  (1) 2022.09.29
Shortest Word  (0) 2022.09.29