Skip to content

GH-285: solve Leetcode 344, 345 #303

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
Apr 19, 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 @@ -386,8 +386,8 @@
<td>341</td>
<td>342</td>
<td>343</td>
<td>344</td>
<td>345</td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode344'>344</a></td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode345'>345</a></td>
<td>346</td>
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode347'>347</a></td>
<td>348</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 @@ -115,6 +115,12 @@ problems:
- name: 283
languages: python
level: easy
- name: 344
languages: python
level: easy
- name: 345
languages: python
level: easy
- name: 347
languages: cpp
level: medium
Expand Down
11 changes: 6 additions & 5 deletions collections/leetcode/generate_readme.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import yaml
import os
from os.path import join

import yaml

wd = os.path.dirname(os.path.realpath(__file__))
# read template file
with open(join(wd, 'README.template.md'), 'r') as f:
tempalte = f.read()
with open(join(wd, 'README.template.md'), 'r', encoding='utf-8') as f:
template = f.read()

# read data.yaml
with open(join(wd, 'data.yaml'), 'r') as f:
Expand Down Expand Up @@ -39,6 +40,6 @@
table += td
table += "</table>"

with open(join(wd, 'README.md'), 'w') as f:
content = tempalte.replace('<!-- table -->', table)
with open(join(wd, 'README.md'), 'w', encoding='utf-8') as f:
content = template.replace('<!-- table -->', table)
f.write(content)
3 changes: 3 additions & 0 deletions problems/leetcode344/.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/leetcode344/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/leetcode344/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"h","e","l","l","o"
1 change: 1 addition & 0 deletions problems/leetcode344/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"o","l","l","e","h"
1 change: 1 addition & 0 deletions problems/leetcode344/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"H","a","n","n","a","h"
1 change: 1 addition & 0 deletions problems/leetcode344/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"h","a","n","n","a","H"
8 changes: 8 additions & 0 deletions problems/leetcode344/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "344. Reverse String",
"link": "https://leetcode.com/problems/reverse-string/",
"tags": [
"Two Pointers",
"String"
]
}
13 changes: 13 additions & 0 deletions problems/leetcode344/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import List


class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
len_s = len(s)
for i in range(0, len_s//2):
temp = s[i]
s[i] = s[len_s - 1 - i]
s[len_s - 1 - i] = temp
46 changes: 46 additions & 0 deletions problems/leetcode344/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:
input_string = f.readline().strip().strip('][').split(',')
return input_string

def read_output(output_file):
with open(output_file, 'r') as f:
output_string = f.readline().strip().strip('][').split(',')
return output_string

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

def gen_test(input_file, output_file):
def test(self):
solution = Solution()
input_string = read_input(input_file)
output_string = read_output(output_file)
solution.reverseString(input_string)
self.assertEqual(input_string, output_string)
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/leetcode345/.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/leetcode345/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/leetcode345/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
1 change: 1 addition & 0 deletions problems/leetcode345/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
holle
1 change: 1 addition & 0 deletions problems/leetcode345/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
leetcode
1 change: 1 addition & 0 deletions problems/leetcode345/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
leotcede
8 changes: 8 additions & 0 deletions problems/leetcode345/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "345. Reverse Vowels of a String",
"link": "https://leetcode.com/problems/reverse-vowels-of-a-string/",
"tags": [
"Two Pointers",
"String"
]
}
17 changes: 17 additions & 0 deletions problems/leetcode345/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def reverseVowels(self, s: str) -> str:
vowels = ['a', 'e', 'i', 'o','u']
s = list(s)
left = 0
right = len(s) - 1
while left < right:
if s[left].lower() in vowels and s[right].lower() in vowels:
s[left], s[right] = s[right], s[left]
right -= 1
left += 1
continue
if s[left].lower() not in vowels:
left += 1
if s[right].lower() not in vowels:
right -= 1
return ''.join(s)
46 changes: 46 additions & 0 deletions problems/leetcode345/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:
input = f.readline().strip()
return input

def read_output(input_file):
with open(input_file, 'r') as f:
output = f.readline().strip()
return output

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

def gen_test(input_file, output_file):
def test(self):
solution = Solution()
input = read_input(input_file)
output = read_output(output_file)
actual = solution.reverseVowels(input)
self.assertEqual(actual, output)
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()