-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
57 lines (35 loc) · 964 Bytes
/
day2.py
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
50
51
52
53
54
55
56
# class Solution(object):
# def isHappy(self, n):
# num = []
# num = splitIntoArray(n)
# while sumsquare(num) != 1:
# n = sumsquare(num)
# num = splitIntoArray(n)
# return True
# def sumsquare(numdigits):
# sumdigits = 0
# for i in range(len(numdigits)):
# sumdigits += numdigits[i] ** 2
# return sumdigits
# def splitIntoArray(num):
# numdigits = [int(i) for i in str(num)]
# return numdigits
#****************************************
#HAPPY NUMBER
#Input: 19
#Output: true
# Explanation:
# 12 + 92 = 82
# 82 + 22 = 68
# 62 + 82 = 100
# 12 + 02 + 02 = 1
class Solution(object):
def isHappy(self, n):
s = {1}
while n not in s:
s.add(n)
n = sumsquare(n)
return n == 1
def sumsquare(n):
n = sum(i * i for i in map(int, str(n)))
return n