From 381d35bebfcc23058491dbc179b428778ba79d6b Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 25 Apr 2025 04:22:12 -0600 Subject: [PATCH] number of 1 bits --- .../top_150_questions_round_15/number_of_1_bits.py | 6 ++++++ .../test_number_of_1_bits_round_15.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_15/number_of_1_bits.py create mode 100644 tests/test_150_questions_round_15/test_number_of_1_bits_round_15.py diff --git a/src/my_project/interviews/top_150_questions_round_15/number_of_1_bits.py b/src/my_project/interviews/top_150_questions_round_15/number_of_1_bits.py new file mode 100644 index 00000000..f1cfb550 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_15/number_of_1_bits.py @@ -0,0 +1,6 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def hammingWeight(self, n: int) -> int: + return bin(n).count('1') \ No newline at end of file diff --git a/tests/test_150_questions_round_15/test_number_of_1_bits_round_15.py b/tests/test_150_questions_round_15/test_number_of_1_bits_round_15.py new file mode 100644 index 00000000..c3e2e903 --- /dev/null +++ b/tests/test_150_questions_round_15/test_number_of_1_bits_round_15.py @@ -0,0 +1,12 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_15\ +.number_of_1_bits import Solution + + +class NumberOfOnesTestCase(unittest.TestCase): + + def test_number_of_ones(self): + solution = Solution() + output = solution.hammingWeight(n=11) + target = 3 + self.assertEqual(output, target)