diff --git a/src/my_project/interviews/top_150_questions_round_11/happy_number.py b/src/my_project/interviews/top_150_questions_round_11/happy_number.py new file mode 100644 index 00000000..0dbfc96f --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_11/happy_number.py @@ -0,0 +1,18 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution(object): + def isHappy(self, n): + + set_answer = set() + + while n != 1: + + if n in set_answer: + return False + else: + set_answer.add(n) + + n = sum([int(c)**2 for c in str(n)]) + + return True \ No newline at end of file diff --git a/tests/test_150_questions_round_11/test_happy_number_round_11.py b/tests/test_150_questions_round_11/test_happy_number_round_11.py new file mode 100644 index 00000000..3e1d8df7 --- /dev/null +++ b/tests/test_150_questions_round_11/test_happy_number_round_11.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_11\ +.happy_number import Solution + +class HappyNumberTestCase(unittest.TestCase): + + def test_is_happy_number(self): + solution = Solution() + output = solution.isHappy(n=19) + self.assertTrue(output) + + def test_is_no_happy_number(self): + solution = Solution() + output = solution.isHappy(n=2) + self.assertFalse(output) \ No newline at end of file