From 0e59bcd3c885834af7bd14e67d7353233414ce76 Mon Sep 17 00:00:00 2001 From: Priyansh Vaish Date: Sun, 21 Dec 2025 00:08:00 +0530 Subject: [PATCH 1/6] Create pascals_Triangle.py --- data_structures/arrays/pascals_Triangle.py | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 data_structures/arrays/pascals_Triangle.py diff --git a/data_structures/arrays/pascals_Triangle.py b/data_structures/arrays/pascals_Triangle.py new file mode 100644 index 000000000000..ed4a3d7cd06c --- /dev/null +++ b/data_structures/arrays/pascals_Triangle.py @@ -0,0 +1,42 @@ +""" Generate Pascal's Triangle. + +Python doctest can be run with the following command: python -m doctest -v pascals_triangle.py + +Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. + +In Pascal's triangle, each number is the sum of the two numbers directly above it. + +Example Input: numRows = 5 Output: [ [1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1] ] """ + +class Solution(object): def generate(self, numRows): """ Generate the first numRows of Pascal's triangle. + + Args: + numRows (int): The number of rows to generate. + + Returns: + list[list[int]]: Pascal's triangle as a list of lists. + + Examples: + >>> solution = Solution() + >>> solution.generate(5) + [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]] + >>> solution.generate(1) + [[1]] + >>> solution.generate(0) + [] + """ + ans = [] + for i in range(numRows): + # Initialize the row with 1s + row = [1] * (i + 1) + + # Compute inner elements by summing elements from the previous row + for j in range(1, i): + row[j] = ans[i-1][j-1] + ans[i-1][j] + + ans.append(row) + return ans +if name == "main": + import doctest + +doctest.testmod() From fede4f1afb80bcb48e39820e5b35e797a2b90785 Mon Sep 17 00:00:00 2001 From: Priyansh Vaish Date: Sun, 21 Dec 2025 00:14:47 +0530 Subject: [PATCH 2/6] Rename pascals_Triangle.py to pascals_triangle.py --- .../arrays/{pascals_Triangle.py => pascals_triangle.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename data_structures/arrays/{pascals_Triangle.py => pascals_triangle.py} (100%) diff --git a/data_structures/arrays/pascals_Triangle.py b/data_structures/arrays/pascals_triangle.py similarity index 100% rename from data_structures/arrays/pascals_Triangle.py rename to data_structures/arrays/pascals_triangle.py From c4f6fa51897fd60cb20e9ff7669c2c9453099c8b Mon Sep 17 00:00:00 2001 From: Priyansh Vaish Date: Sun, 21 Dec 2025 00:15:29 +0530 Subject: [PATCH 3/6] Update pascals_triangle.py --- data_structures/arrays/pascals_triangle.py | 79 ++++++++++++---------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/data_structures/arrays/pascals_triangle.py b/data_structures/arrays/pascals_triangle.py index ed4a3d7cd06c..e114345998bd 100644 --- a/data_structures/arrays/pascals_triangle.py +++ b/data_structures/arrays/pascals_triangle.py @@ -1,42 +1,47 @@ -""" Generate Pascal's Triangle. +""" +Generate Pascal's Triangle. -Python doctest can be run with the following command: python -m doctest -v pascals_triangle.py +Reference: https://en.wikipedia.org/wiki/Pascal%27s_triangle -Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. +Python doctest can be run with the following command: +python -m doctest -v pascals_triangle.py +Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. - -Example Input: numRows = 5 Output: [ [1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1] ] """ - -class Solution(object): def generate(self, numRows): """ Generate the first numRows of Pascal's triangle. - - Args: - numRows (int): The number of rows to generate. - - Returns: - list[list[int]]: Pascal's triangle as a list of lists. - - Examples: - >>> solution = Solution() - >>> solution.generate(5) - [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]] - >>> solution.generate(1) - [[1]] - >>> solution.generate(0) - [] - """ - ans = [] - for i in range(numRows): - # Initialize the row with 1s - row = [1] * (i + 1) - - # Compute inner elements by summing elements from the previous row - for j in range(1, i): - row[j] = ans[i-1][j-1] + ans[i-1][j] +""" + +class Solution: + def generate(self, num_rows: int) -> list[list[int]]: + """ + Generate the first num_rows of Pascal's triangle. + + Args: + num_rows (int): The number of rows to generate. + + Returns: + list[list[int]]: Pascal's triangle as a list of lists. + + Examples: + >>> solution = Solution() + >>> solution.generate(5) + [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]] + >>> solution.generate(1) + [[1]] + >>> solution.generate(0) + [] + """ + ans: list[list[int]] = [] + for i in range(num_rows): + # Initialize the row with 1s + row = [1] * (i + 1) - ans.append(row) - return ans -if name == "main": - import doctest - -doctest.testmod() + # Compute inner elements by summing elements from the previous row + for j in range(1, i): + row[j] = ans[i-1][j-1] + ans[i-1][j] + + ans.append(row) + return ans + +if __name__ == "__main__": + import doctest + doctest.testmod() From 73097880484fedf3ccf3c27b6aa21798a51bc439 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 Dec 2025 18:49:59 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/pascals_triangle.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/pascals_triangle.py b/data_structures/arrays/pascals_triangle.py index e114345998bd..9f0455ab55b9 100644 --- a/data_structures/arrays/pascals_triangle.py +++ b/data_structures/arrays/pascals_triangle.py @@ -10,6 +10,7 @@ In Pascal's triangle, each number is the sum of the two numbers directly above it. """ + class Solution: def generate(self, num_rows: int) -> list[list[int]]: """ @@ -34,14 +35,16 @@ def generate(self, num_rows: int) -> list[list[int]]: for i in range(num_rows): # Initialize the row with 1s row = [1] * (i + 1) - + # Compute inner elements by summing elements from the previous row for j in range(1, i): - row[j] = ans[i-1][j-1] + ans[i-1][j] - + row[j] = ans[i - 1][j - 1] + ans[i - 1][j] + ans.append(row) return ans + if __name__ == "__main__": import doctest + doctest.testmod() From 227bc2942ef4e5640013ad7d4269b4909c3c167e Mon Sep 17 00:00:00 2001 From: Priyansh Vaish Date: Sun, 21 Dec 2025 00:32:30 +0530 Subject: [PATCH 5/6] Update pascals_triangle.py --- data_structures/arrays/pascals_triangle.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/data_structures/arrays/pascals_triangle.py b/data_structures/arrays/pascals_triangle.py index 9f0455ab55b9..649c8a0e060d 100644 --- a/data_structures/arrays/pascals_triangle.py +++ b/data_structures/arrays/pascals_triangle.py @@ -6,11 +6,10 @@ Python doctest can be run with the following command: python -m doctest -v pascals_triangle.py -Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. +Given a non-negative integer num_rows, generate the first num_rows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. """ - class Solution: def generate(self, num_rows: int) -> list[list[int]]: """ @@ -35,16 +34,12 @@ def generate(self, num_rows: int) -> list[list[int]]: for i in range(num_rows): # Initialize the row with 1s row = [1] * (i + 1) - # Compute inner elements by summing elements from the previous row for j in range(1, i): - row[j] = ans[i - 1][j - 1] + ans[i - 1][j] - + row[j] = ans[i-1][j-1] + ans[i-1][j] ans.append(row) return ans - if __name__ == "__main__": import doctest - doctest.testmod() From cfc991681b69c514c5ed1d40e841ff151f522f30 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 Dec 2025 19:03:24 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/pascals_triangle.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/pascals_triangle.py b/data_structures/arrays/pascals_triangle.py index 649c8a0e060d..733cad93cedb 100644 --- a/data_structures/arrays/pascals_triangle.py +++ b/data_structures/arrays/pascals_triangle.py @@ -10,6 +10,7 @@ In Pascal's triangle, each number is the sum of the two numbers directly above it. """ + class Solution: def generate(self, num_rows: int) -> list[list[int]]: """ @@ -36,10 +37,12 @@ def generate(self, num_rows: int) -> list[list[int]]: row = [1] * (i + 1) # Compute inner elements by summing elements from the previous row for j in range(1, i): - row[j] = ans[i-1][j-1] + ans[i-1][j] + row[j] = ans[i - 1][j - 1] + ans[i - 1][j] ans.append(row) return ans + if __name__ == "__main__": import doctest + doctest.testmod()