Skip to content

Commit ca99ac2

Browse files
authored
GH-285: solve Leetcode 136, 389 (#312)
1 parent 70a827b commit ca99ac2

File tree

20 files changed

+172
-2
lines changed

20 files changed

+172
-2
lines changed

collections/leetcode/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode133'>133</a></td>
158158
<td>134</td>
159159
<td>135</td>
160-
<td>136</td>
160+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode136'>136</a></td>
161161
<td>137</td>
162162
<td>138</td>
163163
<td>139</td>
@@ -435,7 +435,7 @@
435435
<td>386</td>
436436
<td>387</td>
437437
<td>388</td>
438-
<td>389</td>
438+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode389'>389</a></td>
439439
<td>390</td>
440440
<tr>
441441
<td>391</td>

collections/leetcode/data.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ problems:
7878
- name: 133
7979
languages: cpp
8080
level: medium
81+
- name: 136
82+
languages: python
83+
level: easy
8184
- name: 141
8285
languages: python
8386
level: easy
@@ -136,6 +139,9 @@ problems:
136139
- name: 347
137140
languages: cpp
138141
level: medium
142+
- name: 389
143+
languages: python
144+
level: easy
139145
- name: 392
140146
languages: cpp
141147
level: easy

problems/leetcode136/.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/leetcode136/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/leetcode136/data/1.in

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

problems/leetcode136/data/1.out

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

problems/leetcode136/data/2.in

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

problems/leetcode136/data/2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "Leetcode 136. Single Number",
3+
"link": "https://leetcode.com/problems/single-number/",
4+
"tags": [
5+
"Array",
6+
"Bit Manipulation"
7+
]
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def singleNumber(self, nums: List[int]) -> int:
6+
result = 0
7+
for num in nums:
8+
result ^= num
9+
return result
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
nums = [int(num) if num != '_' else None for num in f.readline().strip().strip('][').split(',') ]
16+
return nums
17+
18+
def read_output(output_file):
19+
with open(output_file, 'r') as f:
20+
k = int(f.readline().strip())
21+
return k
22+
23+
class TestSequenceMeta(type):
24+
def __new__(mcs, name, bases, dict):
25+
26+
def gen_test(input_file, output_file):
27+
def test(self):
28+
solution = Solution()
29+
nums = read_input(input_file)
30+
expected_k = read_output(output_file)
31+
actual_k = solution.singleNumber(nums)
32+
self.assertEqual(actual_k, expected_k)
33+
return test
34+
35+
for test_case in test_cases:
36+
test_name = f'test_{test_case}'
37+
input_file = join(test_data_directory, f'{test_case}.in')
38+
output_file = join(test_data_directory, f'{test_case}.out')
39+
dict[test_name] = gen_test(input_file, output_file)
40+
return type.__new__(mcs, name, bases, dict)
41+
42+
class TestSequence(unittest.TestCase, metaclass=TestSequenceMeta):
43+
pass
44+
45+
if __name__ == '__main__':
46+
unittest.main()

problems/leetcode389/.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/leetcode389/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/leetcode389/data/1.in

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

problems/leetcode389/data/1.out

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

problems/leetcode389/data/2.in

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

problems/leetcode389/data/2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
y
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "Leetcode 389. Find the Difference",
3+
"link": "https://leetcode.com/problems/find-the-difference/",
4+
"tags": [
5+
"Hash Table",
6+
"String",
7+
"Bit Manipulation",
8+
"Sorting"
9+
]
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def findTheDifference(self, s: str, t: str) -> str:
3+
c = 0
4+
for cs in s:
5+
c ^= ord(cs)
6+
for ct in t:
7+
c ^= ord(ct)
8+
return chr(c)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
s = f.readline().strip()
16+
t = f.readline().strip()
17+
return s, t
18+
19+
def read_output(output_file):
20+
with open(output_file, 'r') as f:
21+
k = f.readline().strip()
22+
return k
23+
24+
class TestSequenceMeta(type):
25+
def __new__(mcs, name, bases, dict):
26+
27+
def gen_test(input_file, output_file):
28+
def test(self):
29+
solution = Solution()
30+
s, t = read_input(input_file)
31+
expected_k = read_output(output_file)
32+
actual_k = solution.findTheDifference(s, t)
33+
self.assertEqual(actual_k, expected_k)
34+
return test
35+
36+
for test_case in test_cases:
37+
test_name = f'test_{test_case}'
38+
input_file = join(test_data_directory, f'{test_case}.in')
39+
output_file = join(test_data_directory, f'{test_case}.out')
40+
dict[test_name] = gen_test(input_file, output_file)
41+
return type.__new__(mcs, name, bases, dict)
42+
43+
class TestSequence(unittest.TestCase, metaclass=TestSequenceMeta):
44+
pass
45+
46+
if __name__ == '__main__':
47+
unittest.main()

0 commit comments

Comments
 (0)