From 6f504a99aad59bd785815a547db20e75c2109f12 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 31 Jan 2025 04:21:59 -0600 Subject: [PATCH] adding happy number --- .../happy_number.py | 19 +++++++++++++++++++ .../test_happy_number_round_13.py | 15 +++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_13/happy_number.py create mode 100644 tests/test_150_questions_round_13/test_happy_number_round_13.py diff --git a/src/my_project/interviews/top_150_questions_round_13/happy_number.py b/src/my_project/interviews/top_150_questions_round_13/happy_number.py new file mode 100644 index 00000000..1fe282df --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_13/happy_number.py @@ -0,0 +1,19 @@ +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 diff --git a/tests/test_150_questions_round_13/test_happy_number_round_13.py b/tests/test_150_questions_round_13/test_happy_number_round_13.py new file mode 100644 index 00000000..8bcb494a --- /dev/null +++ b/tests/test_150_questions_round_13/test_happy_number_round_13.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_13\ +.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