Skip to content

GH-285: solve Leetcode 141, 283 #295

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 9, 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
762 changes: 7 additions & 755 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions collections/leetcode/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ problems:
- name: 133
languages: cpp
level: medium
- name: 141
languages: python
level: easy
- name: 142
languages: cpp
level: medium
Expand Down Expand Up @@ -109,6 +112,9 @@ problems:
- name: 278
languages: cpp
level: easy
- name: 283
languages: python
level: easy
- name: 347
languages: cpp
level: medium
Expand Down
3 changes: 3 additions & 0 deletions problems/leetcode141/.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/leetcode141/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/leetcode141/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3,2,0,-4
1
1 change: 1 addition & 0 deletions problems/leetcode141/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
2 changes: 2 additions & 0 deletions problems/leetcode141/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1,2
0
1 change: 1 addition & 0 deletions problems/leetcode141/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
2 changes: 2 additions & 0 deletions problems/leetcode141/data/3.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
-1
1 change: 1 addition & 0 deletions problems/leetcode141/data/3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
9 changes: 9 additions & 0 deletions problems/leetcode141/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "141. Linked List Cycle",
"link": "https://leetcode.com/problems/linked-list-cycle/",
"tags": [
"Hash Table",
"Linked List",
"Two Pointers"
]
}
17 changes: 17 additions & 0 deletions problems/leetcode141/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import Optional


class ListNode:
def __init__(self, x):
self.val = x
self.next = None

class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
62 changes: 62 additions & 0 deletions problems/leetcode141/python/tests/solution_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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)])

class ListNode:
def __init__(self, x):
self.val = x
self.next = None

def read_input(input_file):
with open(input_file, 'r') as f:
nums = [int(num) if num != '_' else None for num in f.readline().strip().strip('][').split(',') ]
val = int(f.readline().strip())
current = head = ListNode(0)
loop_node = None
for i, num in enumerate(nums):
current.next = ListNode(num)
current = current.next
if i == val:
loop_node = current
if loop_node is not None:
current.next = loop_node
return head.next, val

def read_output(output_file):
with open(output_file, 'r') as f:
k = f.readline().strip()
k = False if k == 'false' else True
return k

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

def gen_test(input_file, output_file):
def test(self):
solution = Solution()
head, val = read_input(input_file)
expected_k = read_output(output_file)
actual_k = solution.hasCycle(head)
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/leetcode283/.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/leetcode283/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/leetcode283/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0,1,0,3,12
1 change: 1 addition & 0 deletions problems/leetcode283/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1,3,12,0,0
1 change: 1 addition & 0 deletions problems/leetcode283/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
1 change: 1 addition & 0 deletions problems/leetcode283/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
8 changes: 8 additions & 0 deletions problems/leetcode283/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "283. Move Zeroes",
"link": "https://leetcode.com/problems/move-zeroes/",
"tags": [
"Array",
"Two Pointers"
]
}
15 changes: 15 additions & 0 deletions problems/leetcode283/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import List


class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
slow = 0
for fast in range(len(nums)):
if nums[fast] != 0 and nums[slow] == 0:
nums[slow], nums[fast] = nums[fast], nums[slow]

if nums[slow] != 0:
slow += 1
46 changes: 46 additions & 0 deletions problems/leetcode283/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:
nums = [int(num) if num != '_' else None for num in f.readline().strip().strip('][').split(',') ]
return nums

def read_output(output_file):
with open(output_file, 'r') as f:
nums = [int(num) if num != '_' else None for num in f.readline().strip().strip('][').split(',') ]
return nums

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

def gen_test(input_file, output_file):
def test(self):
solution = Solution()
input_nums = read_input(input_file)
expected_mums = read_output(output_file)
solution.moveZeroes(input_nums)
self.assertEqual(input_nums, expected_mums)
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()