aboutsummaryrefslogtreecommitdiff
path: root/challenge-163/paulo-custodio/python/ch-1.py
blob: 67a949985e3601976eaedd0237e93747b1cf763c (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
#!/usr/bin/env python3

# Challenge 163
#
# Task 1: Sum Bitwise Operator
# Submitted by: Mohammad S Anwar
#
# You are given list positive numbers, @n.
#
# Write script to calculate the sum of bitwise & operator for all unique pairs.
# Example 1
#
# Input: @n = (1, 2, 3)
# Output: 3
#
# Since (1 & 2) + (2 & 3) + (1 & 3) => 0 + 2 + 1 =>  3.
#
# Example 2
#
# Input: @n = (2, 3, 4)
# Output: 2
#
# Since (2 & 3) + (2 & 4) + (3 & 4) => 2 + 0 + 0 =>  2.

def sum_biwise_and(*n):
    total_sum = 0
    for i in range(len(n) - 1):
        for j in range(i + 1, len(n)):
            total_sum += n[i] & n[j]
    return total_sum

import sys
print(sum_biwise_and(*map(int, sys.argv[1:])))