Skip to content

GH-313: solve leetcode 202 #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion collections/leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
<td>200</td>
<tr>
<td>201</td>
<td>202</td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode202'>202</a></td>
<td>203</td>
<td>204</td>
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode205'>205</a></td>
Expand Down
3 changes: 3 additions & 0 deletions collections/leetcode/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ problems:
- name: 169
languages: cpp
level: easy
- name: 202
languages: python
level: easy
- name: 205
languages: cpp
level: medium
Expand Down
3 changes: 3 additions & 0 deletions problems/leetcode202/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
solution
*.dSYM
python/main/__pycache__
10 changes: 10 additions & 0 deletions problems/leetcode202/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Leetcode Problem

## Usage

Test program

```
# Run all tests
python solution_test.py
```
1 change: 1 addition & 0 deletions problems/leetcode202/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
19
1 change: 1 addition & 0 deletions problems/leetcode202/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
True
1 change: 1 addition & 0 deletions problems/leetcode202/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
1 change: 1 addition & 0 deletions problems/leetcode202/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
False
9 changes: 9 additions & 0 deletions problems/leetcode202/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Leetcode 202. Happy Number",
"link": "https://leetcode.com/problems/happy-number/",
"tags": [
"Hash Table",
"Math",
"Two Pointers"
]
}
20 changes: 20 additions & 0 deletions problems/leetcode202/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
def get_next(n):
total_sum = 0
while n > 0:
n, digit = divmod(n, 10)
total_sum += digit ** 2
return total_sum

seen = set()
while n != 1 and n not in seen:
seen.add(n)
n = get_next(n)

return n == 1

49 changes: 49 additions & 0 deletions problems/leetcode202/python/tests/solution_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sys
import unittest
from os import listdir
from os.path import abspath, dirname, join

python_source = dirname(dirname(abspath(__file__)))
sys.path.append(python_source)
from main.solution import Solution

test_data_directory = join(dirname(python_source), 'data')
test_cases = set([data_file.split('.')[0] for data_file in listdir(test_data_directory)])

def read_input(input_file):
with open(input_file, 'r') as f:
val = int(f.readline().strip())
return val

def read_output(output_file):
with open(output_file, 'r') as f:
k = f.readline().strip()
if k == 'True':
return True
else:
return False

class TestSequenceMeta(type):
def __new__(mcs, name, bases, dict):

def gen_test(input_file, output_file):
def test(self):
solution = Solution()
val = read_input(input_file)
expected_k = read_output(output_file)
actual_k = solution.isHappy(val)
self.assertEqual(actual_k, expected_k)
return test

for test_case in test_cases:
test_name = f'test_{test_case}'
input_file = join(test_data_directory, f'{test_case}.in')
output_file = join(test_data_directory, f'{test_case}.out')
dict[test_name] = gen_test(input_file, output_file)
return type.__new__(mcs, name, bases, dict)

class TestSequence(unittest.TestCase, metaclass=TestSequenceMeta):
pass

if __name__ == '__main__':
unittest.main()