diff --git a/src/my_project/interviews/top_150_questions_round_20/happy_number.py b/src/my_project/interviews/top_150_questions_round_20/happy_number.py new file mode 100644 index 00000000..72982656 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_20/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 not in set_answer: + set_answer.add(n) + else: + return False + + 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_20/test_happy_number_round_20.py b/tests/test_150_questions_round_20/test_happy_number_round_20.py new file mode 100644 index 00000000..f1dc1135 --- /dev/null +++ b/tests/test_150_questions_round_20/test_happy_number_round_20.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_20\ +.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