Skip to content

Commit e98e5bd

Browse files
authored
GH-313: Solve Leetcode 118 (#317)
1 parent 70a3d8f commit e98e5bd

File tree

11 files changed

+87
-1
lines changed

11 files changed

+87
-1
lines changed

collections/leetcode/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
<td>115</td>
138138
<td>116</td>
139139
<td>117</td>
140-
<td>118</td>
140+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode118'>118</a></td>
141141
<td>119</td>
142142
<td>120</td>
143143
<tr>

collections/leetcode/data.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ problems:
6666
- name: 106
6767
languages: cpp
6868
level: medium
69+
- name: 118
70+
languages: cpp
71+
level: easy
6972
- name: 121
7073
languages: cpp
7174
level: easy

problems/leetcode118/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
solution
2+
*.dSYM
3+
python/main/__pycache__

problems/leetcode118/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Leetcode Problem
2+
3+
## Usage
4+
5+
Test program
6+
7+
```
8+
# Run all tests
9+
python solution_test.py
10+
```

problems/leetcode118/data/1.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5

problems/leetcode118/data/1.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

problems/leetcode118/data/2.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

problems/leetcode118/data/2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[[1]]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "118. Pascal's Triangle",
3+
"link": "https://leetcode.com/problems/pascals-triangle/",
4+
"tags": [
5+
"Array",
6+
"Dynamic Programming"
7+
]
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def generate(self, numRows: int) -> List[List[int]]:
6+
ans = []
7+
for i in range(numRows):
8+
ans.append([1] * (i + 1))
9+
for i in range(2, numRows):
10+
for j in range(1, len(ans[i]) - 1):
11+
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j]
12+
return ans
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
import unittest
3+
from os import listdir
4+
from os.path import abspath, dirname, join
5+
6+
python_source = dirname(dirname(abspath(__file__)))
7+
sys.path.append(python_source)
8+
from main.solution import Solution
9+
10+
test_data_directory = join(dirname(python_source), 'data')
11+
test_cases = set([data_file.split('.')[0] for data_file in listdir(test_data_directory)])
12+
13+
def read_input(input_file):
14+
with open(input_file, 'r') as f:
15+
val = int(f.readline().strip())
16+
return val
17+
18+
def read_output(output_file):
19+
with open(output_file, 'r') as f:
20+
k = f.readline().strip()
21+
return k
22+
23+
class TestSequenceMeta(type):
24+
def __new__(mcs, name, bases, dict):
25+
26+
def gen_test(input_file, output_file):
27+
def test(self):
28+
solution = Solution()
29+
val = read_input(input_file)
30+
expected_k = read_output(output_file)
31+
actual_k = solution.generate(val)
32+
self.assertEqual(f'{actual_k}'.replace(' ',''), expected_k)
33+
return test
34+
35+
for test_case in test_cases:
36+
test_name = f'test_{test_case}'
37+
input_file = join(test_data_directory, f'{test_case}.in')
38+
output_file = join(test_data_directory, f'{test_case}.out')
39+
dict[test_name] = gen_test(input_file, output_file)
40+
return type.__new__(mcs, name, bases, dict)
41+
42+
class TestSequence(unittest.TestCase, metaclass=TestSequenceMeta):
43+
pass
44+
45+
if __name__ == '__main__':
46+
unittest.main()

0 commit comments

Comments
 (0)