Skip to content

GH-285: solve leetcode 168, 925 #306

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 3 commits into from
May 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 @@ -192,7 +192,7 @@
<td>165</td>
<td>166</td>
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode167'>167</a></td>
<td>168</td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode168'>168</a></td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode169'>169</a></td>
<td>170</td>
<tr>
Expand Down Expand Up @@ -1025,7 +1025,7 @@
<td>922</td>
<td>923</td>
<td>924</td>
<td>925</td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode925'>925</a></td>
<td>926</td>
<td>927</td>
<td>928</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 @@ -84,6 +84,9 @@ problems:
- name: 167
languages: cpp
level: medium
- name: 168
languages: python
level: easy
- name: 169
languages: cpp
level: easy
Expand Down Expand Up @@ -148,6 +151,9 @@ problems:
- name: 881
languages: cpp
level: medium
- name: 925
languages: python
level: easy
- name: 958
languages: cpp
level: medium
Expand Down
3 changes: 3 additions & 0 deletions problems/leetcode168/.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/leetcode168/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/leetcode168/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions problems/leetcode168/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A
1 change: 1 addition & 0 deletions problems/leetcode168/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
28
1 change: 1 addition & 0 deletions problems/leetcode168/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AB
1 change: 1 addition & 0 deletions problems/leetcode168/data/3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
701
1 change: 1 addition & 0 deletions problems/leetcode168/data/3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ZY
8 changes: 8 additions & 0 deletions problems/leetcode168/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Leetcode 168. Excel Sheet Column Title",
"link": "https://leetcode.com/problems/excel-sheet-column-title/",
"tags": [
"Math",
"String"
]
}
9 changes: 9 additions & 0 deletions problems/leetcode168/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def convertToTitle(self, num):
capitals = [chr(x) for x in range(ord('A'), ord('Z')+1)]
result = []
while num > 0:
result.append(capitals[(num-1)%26])
num = (num-1) // 26
result.reverse()
return ''.join(result)
46 changes: 46 additions & 0 deletions problems/leetcode168/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.convertToTitle(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()
2 changes: 1 addition & 1 deletion problems/leetcode345/problem_infos.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "345. Reverse Vowels of a String",
"name": "Leetcode 345. Reverse Vowels of a String",
"link": "https://leetcode.com/problems/reverse-vowels-of-a-string/",
"tags": [
"Two Pointers",
Expand Down
3 changes: 3 additions & 0 deletions problems/leetcode925/.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/leetcode925/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
```
2 changes: 2 additions & 0 deletions problems/leetcode925/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alex
aaleex
1 change: 1 addition & 0 deletions problems/leetcode925/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
True
2 changes: 2 additions & 0 deletions problems/leetcode925/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
saeed
ssaaedd
1 change: 1 addition & 0 deletions problems/leetcode925/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
False
8 changes: 8 additions & 0 deletions problems/leetcode925/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Leetcode 925. Long Pressed Name",
"link": "https://leetcode.com/problems/long-pressed-name/",
"tags": [
"Two Pointer",
"String"
]
}
9 changes: 9 additions & 0 deletions problems/leetcode925/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
j = 0
for i in range(len(typed)):
if j < len(name) and name[j] == typed[i]:
j += 1
elif i == 0 or typed[i] != typed[i-1]:
return False
return j == len(name)
50 changes: 50 additions & 0 deletions problems/leetcode925/python/tests/solution_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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:
name = f.readline().strip()
typed = f.readline().strip()
return name, typed

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()
name, typed = read_input(input_file)
expected_k = read_output(output_file)
actual_k = solution.isLongPressedName(name, typed)
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()