From 4e5ee4dbe720d1f68d3dbced5c9f68d90add5834 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 21 Nov 2024 04:35:36 -0600 Subject: [PATCH] adding happy number --- .../top_150_questions_round_11/happy_number.py | 18 ++++++++++++++++++ .../test_happy_number_round_11.py | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_11/happy_number.py create mode 100644 tests/test_150_questions_round_11/test_happy_number_round_11.py 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