aboutsummaryrefslogtreecommitdiff
path: root/challenge-258/luca-ferrari/python/ch-2.py
blob: a5f6a9717723d8494d8ae81ecbd83695f1c39818 (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
31
32
33
34
35
36
#!python

#
# Perl Weekly Challenge 258
# Task 2
#
# See <https://perlweeklychallenge.org/blog/perl-weekly-challenge-258>
#

import sys


# task implementation
# the return value will be printed
def task_2( args ):
    k = int( args[ 0 ] )
    nums = list( map( int, args[ 1: ] ) )

    def is_index_ok( v, k ):
        b = '{0:08b}'.format( v )
        count = sum( map( int, list( '{0:08b}'.format( v ) ) ) )
        return count == k

    indexes = list( filter( lambda i: is_index_ok( i, k ),
                            range(0, len( nums ) ) ) )

    summy = 0
    for i in indexes:
        summy += nums[ i ]

    return summy


# invoke the main without the command itself
if __name__ == '__main__':
    print( task_2( sys.argv[ 1: ] ) )