From de7c22813e9c0f8046079be5b129d317b087fcdf Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 3 Dec 2024 04:29:05 -0600 Subject: [PATCH] adding number of one algo --- .../top_150_questions_round_11/number_of_ones.py | 6 ++++++ .../test_number_of_ones_round_11.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_11/number_of_ones.py create mode 100644 tests/test_150_questions_round_11/test_number_of_ones_round_11.py diff --git a/src/my_project/interviews/top_150_questions_round_11/number_of_ones.py b/src/my_project/interviews/top_150_questions_round_11/number_of_ones.py new file mode 100644 index 00000000..f1cfb550 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_11/number_of_ones.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_11/test_number_of_ones_round_11.py b/tests/test_150_questions_round_11/test_number_of_ones_round_11.py new file mode 100644 index 00000000..4bdbb25d --- /dev/null +++ b/tests/test_150_questions_round_11/test_number_of_ones_round_11.py @@ -0,0 +1,12 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_11\ +.number_of_ones 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