Skip to content

GH-313: solve Leetcode 313 #320

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 2 commits into from
Jul 4, 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
4 changes: 2 additions & 2 deletions collections/leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
<td>229</td>
<td>230</td>
<tr>
<td>231</td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode231'>231</a></td>
<td>232</td>
<td>233</td>
<td>234</td>
Expand Down Expand Up @@ -453,7 +453,7 @@
<td>402</td>
<td>403</td>
<td>404</td>
<td>405</td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode405'>405</a></td>
<td>406</td>
<td>407</td>
<td>408</td>
Expand Down
6 changes: 6 additions & 0 deletions collections/leetcode/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ problems:
- name: 217
languages: cpp
level: easy
- name: 231
languages: python
level: easy
- name: 238
languages: cpp
level: medium
Expand Down Expand Up @@ -157,6 +160,9 @@ problems:
- name: 392
languages: cpp
level: easy
- name: 405
languages: python
level: easy
- name: 409
languages: cpp
level: easy
Expand Down
3 changes: 3 additions & 0 deletions problems/leetcode231/.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/leetcode231/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/leetcode231/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions problems/leetcode231/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions problems/leetcode231/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
1 change: 1 addition & 0 deletions problems/leetcode231/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions problems/leetcode231/data/3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
1 change: 1 addition & 0 deletions problems/leetcode231/data/3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
9 changes: 9 additions & 0 deletions problems/leetcode231/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Leetcode 231. Power of Two",
"link": "https://leetcode.com/problems/power-of-two/",
"tags": [
"Math",
"Bit Manipulation",
"Recursion"
]
}
11 changes: 11 additions & 0 deletions problems/leetcode231/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if n == 1:
return True
multiple = 1
while multiple <= n:
multiple = multiple * 2
if multiple == n:
return True
elif multiple > n:
return False
47 changes: 47 additions & 0 deletions problems/leetcode231/python/tests/solution_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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:
text_value = f.readline().strip()
value = True if text_value == 'true' else False
return value

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.isPowerOfTwo(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()
3 changes: 3 additions & 0 deletions problems/leetcode405/.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/leetcode405/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/leetcode405/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
26
1 change: 1 addition & 0 deletions problems/leetcode405/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1a
1 change: 1 addition & 0 deletions problems/leetcode405/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-1
1 change: 1 addition & 0 deletions problems/leetcode405/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ffffffff
8 changes: 8 additions & 0 deletions problems/leetcode405/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "405. Convert a Number to Hexadecimal",
"link": "https://leetcode.com/problems/convert-a-number-to-hexadecimal/",
"tags": [
"Math",
"Bit Manipulation"
]
}
13 changes: 13 additions & 0 deletions problems/leetcode405/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def toHex(self, num: int) -> str:
if num == 0:
return '0'
hex_digits = '0123456789abcdef'
result = ''
if num < 0:
num = num + 2**32
while num != 0:
remainder = num % 16
result = hex_digits[remainder] + result
num = num // 16
return result
46 changes: 46 additions & 0 deletions problems/leetcode405/python/tests/solution_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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()
return k

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.toHex(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()