Skip to content

Commit faa1f30

Browse files
authored
GH-313: solve Leetcode 313 (#320)
1 parent 69a0f0b commit faa1f30

File tree

22 files changed

+178
-2
lines changed

22 files changed

+178
-2
lines changed

collections/leetcode/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@
262262
<td>229</td>
263263
<td>230</td>
264264
<tr>
265-
<td>231</td>
265+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode231'>231</a></td>
266266
<td>232</td>
267267
<td>233</td>
268268
<td>234</td>
@@ -453,7 +453,7 @@
453453
<td>402</td>
454454
<td>403</td>
455455
<td>404</td>
456-
<td>405</td>
456+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode405'>405</a></td>
457457
<td>406</td>
458458
<td>407</td>
459459
<td>408</td>

collections/leetcode/data.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ problems:
130130
- name: 217
131131
languages: cpp
132132
level: easy
133+
- name: 231
134+
languages: python
135+
level: easy
133136
- name: 238
134137
languages: cpp
135138
level: medium
@@ -157,6 +160,9 @@ problems:
157160
- name: 392
158161
languages: cpp
159162
level: easy
163+
- name: 405
164+
languages: python
165+
level: easy
160166
- name: 409
161167
languages: cpp
162168
level: easy

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

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

problems/leetcode231/data/1.out

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

problems/leetcode231/data/2.in

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

problems/leetcode231/data/2.out

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

problems/leetcode231/data/3.in

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

problems/leetcode231/data/3.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 231. Power of Two",
3+
"link": "https://leetcode.com/problems/power-of-two/",
4+
"tags": [
5+
"Math",
6+
"Bit Manipulation",
7+
"Recursion"
8+
]
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isPowerOfTwo(self, n: int) -> bool:
3+
if n == 1:
4+
return True
5+
multiple = 1
6+
while multiple <= n:
7+
multiple = multiple * 2
8+
if multiple == n:
9+
return True
10+
elif multiple > n:
11+
return False
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+
val = int(f.readline().strip())
16+
return val
17+
18+
def read_output(output_file):
19+
with open(output_file, 'r') as f:
20+
text_value = f.readline().strip()
21+
value = True if text_value == 'true' else False
22+
return value
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+
val = read_input(input_file)
31+
expected_k = read_output(output_file)
32+
actual_k = solution.isPowerOfTwo(val)
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()

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

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

problems/leetcode405/data/1.out

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

problems/leetcode405/data/2.in

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

problems/leetcode405/data/2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ffffffff
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "405. Convert a Number to Hexadecimal",
3+
"link": "https://leetcode.com/problems/convert-a-number-to-hexadecimal/",
4+
"tags": [
5+
"Math",
6+
"Bit Manipulation"
7+
]
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def toHex(self, num: int) -> str:
3+
if num == 0:
4+
return '0'
5+
hex_digits = '0123456789abcdef'
6+
result = ''
7+
if num < 0:
8+
num = num + 2**32
9+
while num != 0:
10+
remainder = num % 16
11+
result = hex_digits[remainder] + result
12+
num = num // 16
13+
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+
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+
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+
val = read_input(input_file)
30+
expected_k = read_output(output_file)
31+
actual_k = solution.toHex(val)
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()

0 commit comments

Comments
 (0)