aboutsummaryrefslogtreecommitdiff
path: root/challenge-079/lubos-kolouch/python/ch-1.py
blob: 18cd9792cd568554c2253715ce01bfc269076c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/env python
"""
===============================================================================
#
#         FILE: ch-1.py
#
#        USAGE: ./ch-1.py
#
#  DESCRIPTION: Perl Weekly Challenge Task 1
#               https://perlweeklychallenge.org/blog/perl-weekly-challenge-079/
#               Count set bits
#
#       AUTHOR: Lubos Kolouch
#==============================================================================
"""


def get_dec2bin(what: int):
    """ Get the number of set 1s """

    count = 0
    for i in range(1, what+1):
        mod_str = str(bin(i))[2:]
        count += sum(list(map(int, mod_str)))

    return count


assert get_dec2bin(4) == 5
assert get_dec2bin(3) == 4