Skip to content

Commit cf96783

Browse files
authored
GH-313: solve leetcode 202 (#314)
1 parent ca99ac2 commit cf96783

File tree

11 files changed

+99
-1
lines changed

11 files changed

+99
-1
lines changed

collections/leetcode/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230
<td>200</td>
231231
<tr>
232232
<td>201</td>
233-
<td>202</td>
233+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode202'>202</a></td>
234234
<td>203</td>
235235
<td>204</td>
236236
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode205'>205</a></td>

collections/leetcode/data.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ problems:
102102
- name: 169
103103
languages: cpp
104104
level: easy
105+
- name: 202
106+
languages: python
107+
level: easy
105108
- name: 205
106109
languages: cpp
107110
level: medium

problems/leetcode202/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
solution
2+
*.dSYM
3+
python/main/__pycache__

problems/leetcode202/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Leetcode Problem
2+
3+
## Usage
4+
5+
Test program
6+
7+
```
8+
# Run all tests
9+
python solution_test.py
10+
```

problems/leetcode202/data/1.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
19

problems/leetcode202/data/1.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
True

problems/leetcode202/data/2.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2

problems/leetcode202/data/2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
False
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Leetcode 202. Happy Number",
3+
"link": "https://leetcode.com/problems/happy-number/",
4+
"tags": [
5+
"Hash Table",
6+
"Math",
7+
"Two Pointers"
8+
]
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def isHappy(self, n):
3+
"""
4+
:type n: int
5+
:rtype: bool
6+
"""
7+
def get_next(n):
8+
total_sum = 0
9+
while n > 0:
10+
n, digit = divmod(n, 10)
11+
total_sum += digit ** 2
12+
return total_sum
13+
14+
seen = set()
15+
while n != 1 and n not in seen:
16+
seen.add(n)
17+
n = get_next(n)
18+
19+
return n == 1
20+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import sys
2+
import unittest
3+
from os import listdir
4+
from os.path import abspath, dirname, join
5+
6+
python_source = dirname(dirname(abspath(__file__)))
7+
sys.path.append(python_source)
8+
from main.solution import Solution
9+
10+
test_data_directory = join(dirname(python_source), 'data')
11+
test_cases = set([data_file.split('.')[0] for data_file in listdir(test_data_directory)])
12+
13+
def read_input(input_file):
14+
with open(input_file, 'r') as f:
15+
val = int(f.readline().strip())
16+
return val
17+
18+
def read_output(output_file):
19+
with open(output_file, 'r') as f:
20+
k = f.readline().strip()
21+
if k == 'True':
22+
return True
23+
else:
24+
return False
25+
26+
class TestSequenceMeta(type):
27+
def __new__(mcs, name, bases, dict):
28+
29+
def gen_test(input_file, output_file):
30+
def test(self):
31+
solution = Solution()
32+
val = read_input(input_file)
33+
expected_k = read_output(output_file)
34+
actual_k = solution.isHappy(val)
35+
self.assertEqual(actual_k, expected_k)
36+
return test
37+
38+
for test_case in test_cases:
39+
test_name = f'test_{test_case}'
40+
input_file = join(test_data_directory, f'{test_case}.in')
41+
output_file = join(test_data_directory, f'{test_case}.out')
42+
dict[test_name] = gen_test(input_file, output_file)
43+
return type.__new__(mcs, name, bases, dict)
44+
45+
class TestSequence(unittest.TestCase, metaclass=TestSequenceMeta):
46+
pass
47+
48+
if __name__ == '__main__':
49+
unittest.main()

0 commit comments

Comments
 (0)