From 8091d154c159948b012ce9728cbd8942ea3d148c Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sun, 16 Apr 2023 15:24:13 +0200 Subject: Challenge 057 059 LK Perl Python --- challenge-059/lubos-kolouch/python/ch-2.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenge-059/lubos-kolouch/python/ch-2.py (limited to 'challenge-059/lubos-kolouch/python/ch-2.py') diff --git a/challenge-059/lubos-kolouch/python/ch-2.py b/challenge-059/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..f482a546d6 --- /dev/null +++ b/challenge-059/lubos-kolouch/python/ch-2.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def count_different_bits(a: int, b: int) -> int: + count = 0 + while a > 0 or b > 0: + count += (a % 2) ^ (b % 2) + a >>= 1 + b >>= 1 + return count + +def sum_different_bits(numbers: List[int]) -> int: + total = 0 + for i in range(len(numbers)): + for j in range(i+1, len(numbers)): + total += count_different_bits(numbers[i], numbers[j]) + return total + +if __name__ == "__main__": + input_list = [2, 3, 4] + result = sum_different_bits(input_list) + print(result) -- cgit