Skip to content
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
2 changes: 1 addition & 1 deletion collections/leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
<td>115</td>
<td>116</td>
<td>117</td>
<td>118</td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode118'>118</a></td>
<td>119</td>
<td>120</td>
<tr>
Expand Down
3 changes: 3 additions & 0 deletions collections/leetcode/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ problems:
- name: 106
languages: cpp
level: medium
- name: 118
languages: cpp
level: easy
- name: 121
languages: cpp
level: easy
Expand Down
3 changes: 3 additions & 0 deletions problems/leetcode118/.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/leetcode118/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/leetcode118/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
1 change: 1 addition & 0 deletions problems/leetcode118/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
1 change: 1 addition & 0 deletions problems/leetcode118/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions problems/leetcode118/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[1]]
8 changes: 8 additions & 0 deletions problems/leetcode118/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "118. Pascal's Triangle",
"link": "https://leetcode.com/problems/pascals-triangle/",
"tags": [
"Array",
"Dynamic Programming"
]
}
12 changes: 12 additions & 0 deletions problems/leetcode118/python/main/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import List


class Solution:
def generate(self, numRows: int) -> List[List[int]]:
ans = []
for i in range(numRows):
ans.append([1] * (i + 1))
for i in range(2, numRows):
for j in range(1, len(ans[i]) - 1):
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j]
return ans
46 changes: 46 additions & 0 deletions problems/leetcode118/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.generate(val)
self.assertEqual(f'{actual_k}'.replace(' ',''), 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()