From 5430b10b6e7bd81b1373b9386326be835982838b Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 8 Jan 2025 04:33:47 -0600 Subject: [PATCH] number of 1 bits --- .../top_150_questions_round_12/number_of_1_bits.py | 6 ++++++ .../test_number_of_1_bits_round_12.py | 11 +++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_12/number_of_1_bits.py create mode 100644 tests/test_150_questions_round_12/test_number_of_1_bits_round_12.py diff --git a/src/my_project/interviews/top_150_questions_round_12/number_of_1_bits.py b/src/my_project/interviews/top_150_questions_round_12/number_of_1_bits.py new file mode 100644 index 00000000..f1cfb550 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_12/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_12/test_number_of_1_bits_round_12.py b/tests/test_150_questions_round_12/test_number_of_1_bits_round_12.py new file mode 100644 index 00000000..26243e30 --- /dev/null +++ b/tests/test_150_questions_round_12/test_number_of_1_bits_round_12.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_12\ +.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) \ No newline at end of file