-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path202_happy_number.py
More file actions
49 lines (46 loc) · 1.04 KB
/
Copy path202_happy_number.py
File metadata and controls
49 lines (46 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# see https://en.wikipedia.org/wiki/Happy_number
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
seen = set()
while n > 1 and n not in seen:
seen.add(n)
v = 0
while n > 0:
v += pow(n % 10, 2)
n = n // 10
n = v
return n == 1
vectors = [
1, True,
4, False,
5, False,
7, True,
10, True,
13, True,
19, True,
23, True,
28, True,
31, True,
32, True,
44, True,
49, True,
68, True,
70, True,
79, True,
82, True,
91, True,
94, True,
97, True,
100, True,
130, True
]
for i in range(0, len(vectors), 2):
number = vectors[i]
expected = vectors[i + 1]
print(f'number = {number}')
returned = Solution().isHappy(number)
assert expected == returned, f'for {number} expected {expected}, returned {returned}'