Skip to content

Commit 1f30eef

Browse files
authored
Merge pull request #1310 from ivan1016017/june22
adding happy number
2 parents a952bec + e52d02f commit 1f30eef

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution(object):
5+
def isHappy(self, n):
6+
7+
set_answer = set()
8+
9+
while n != 1:
10+
11+
if n in set_answer:
12+
return False
13+
else:
14+
set_answer.add(n)
15+
16+
n = sum([int(c)**2 for c in str(n)])
17+
18+
return True
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_17\
3+
.happy_number import Solution
4+
5+
class HappyNumberTestCase(unittest.TestCase):
6+
7+
def test_is_happy_number(self):
8+
solution = Solution()
9+
output = solution.isHappy(n=19)
10+
self.assertTrue(output)
11+
12+
def test_is_no_happy_number(self):
13+
solution = Solution()
14+
output = solution.isHappy(n=2)
15+
self.assertFalse(output)

0 commit comments

Comments
 (0)