From d950cd3f7d1617842fc218d882b6fb195cde55e4 Mon Sep 17 00:00:00 2001 From: iamhardikat11 Date: Fri, 10 Dec 2021 14:52:26 +0530 Subject: [PATCH 1/8] My Commit for the Issue #2864 Pascal's Triangle --- .../thealgorithms/maths/PascalTriangle.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/PascalTriangle.java diff --git a/src/main/java/com/thealgorithms/maths/PascalTriangle.java b/src/main/java/com/thealgorithms/maths/PascalTriangle.java new file mode 100644 index 000000000000..9e6a262fea11 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/PascalTriangle.java @@ -0,0 +1,73 @@ +package com.thealgorithms.maths; + +import java.util.Scanner; +public class PascalTriangle { + /** + *In mathematics, Pascal's triangle is a triangular array of the binomial coefficients that arises + * in probability theory, combinatorics, and algebra. In much of the Western world, it is named after + * the French mathematician Blaise Pascal, although other mathematicians studied it centuries before + * him in India, Persia, China, Germany, and Italy. + * + * The rows of Pascal's triangle are conventionally enumerated starting with row n=0 at the top (the 0th row). + * The entries in each row are numbered from the left beginning with k=0 and are usually staggered relative + * to the numbers in the adjacent rows. The triangle may be constructed in the following manner: + * In row 0 (the topmost row), there is a unique nonzero entry 1. Each entry of each subsequent row is + * constructed by adding the number above and to the left with the number above and to the right, treating + * blank entries as 0. For example, the initial number in the first (or any other) row is 1 (the sum of 0 and 1), + * whereas the numbers 1 and 3 in the third row are added to produce the number 4 in the fourth row. * + * + *

+ * link:-https://en.wikipedia.org/wiki/Pascal%27s_triangle + * + *

+ * Example:- + * 1 + * 1 1 + * 1 2 1 + * 1 3 3 1 + * 1 4 6 4 1 + * 1 5 10 10 5 1 + * 1 6 15 20 15 6 1 + * 1 7 21 35 35 21 7 1 + * 1 8 28 56 70 56 28 8 1 + * + */ + + public static void main (String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + printPascal(n); + } + + public static void printPascal(int n) + { + /** + * @param arr An auxiliary array to store generated pascal triangle values + * @return + */ + int[][] arr = new int[n][n]; + + + /** + * @param line Iterate through every line and print integer(s) in it + * @param i Represents the column number of the element we are currently on + */ + for (int line = 0; line < n; line++) + { + /** + * @Every line has number of integers equal to line number + */ + for (int i = 0; i <= line; i++) + { + // First and last values in every row are 1 + if (line == i || i == 0) + arr[line][i] = 1; + // The rest elements are sum of values just above and left of above + else + arr[line][i] = arr[line-1][i-1] + arr[line-1][i]; + System.out.print(arr[line][i]+" "); + } + System.out.println(); + } + } +} \ No newline at end of file From 568215127507bbac0d8110bf3f84390c7d34b3f3 Mon Sep 17 00:00:00 2001 From: Hardik Soni <79035082+iamhardikat11@users.noreply.github.com> Date: Sat, 11 Dec 2021 23:10:56 +0530 Subject: [PATCH 2/8] Update src/main/java/com/thealgorithms/maths/PascalTriangle.java Co-authored-by: Andrii Siriak --- src/main/java/com/thealgorithms/maths/PascalTriangle.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/PascalTriangle.java b/src/main/java/com/thealgorithms/maths/PascalTriangle.java index 9e6a262fea11..594824c05d33 100644 --- a/src/main/java/com/thealgorithms/maths/PascalTriangle.java +++ b/src/main/java/com/thealgorithms/maths/PascalTriangle.java @@ -39,7 +39,7 @@ public static void main (String[] args) { printPascal(n); } - public static void printPascal(int n) + public static int[][] pascal(int n) { /** * @param arr An auxiliary array to store generated pascal triangle values @@ -65,9 +65,9 @@ public static void printPascal(int n) // The rest elements are sum of values just above and left of above else arr[line][i] = arr[line-1][i-1] + arr[line-1][i]; - System.out.print(arr[line][i]+" "); } - System.out.println(); } + + return arr; } } \ No newline at end of file From 7f3100564cdb70e0610e438c102ed883c1b94094 Mon Sep 17 00:00:00 2001 From: Hardik Soni <79035082+iamhardikat11@users.noreply.github.com> Date: Sat, 11 Dec 2021 23:28:12 +0530 Subject: [PATCH 3/8] Update PascalTriangle.java --- src/main/java/com/thealgorithms/maths/PascalTriangle.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/PascalTriangle.java b/src/main/java/com/thealgorithms/maths/PascalTriangle.java index 594824c05d33..823f5d35928a 100644 --- a/src/main/java/com/thealgorithms/maths/PascalTriangle.java +++ b/src/main/java/com/thealgorithms/maths/PascalTriangle.java @@ -39,15 +39,13 @@ public static void main (String[] args) { printPascal(n); } - public static int[][] pascal(int n) + public static int[][] pascal(int n) { /** * @param arr An auxiliary array to store generated pascal triangle values * @return */ int[][] arr = new int[n][n]; - - /** * @param line Iterate through every line and print integer(s) in it * @param i Represents the column number of the element we are currently on @@ -70,4 +68,4 @@ public static int[][] pascal(int n) return arr; } -} \ No newline at end of file +} From c698f285c0a0fec9d6e4acde44fa9288da1c221e Mon Sep 17 00:00:00 2001 From: Hardik Soni <79035082+iamhardikat11@users.noreply.github.com> Date: Sat, 11 Dec 2021 23:32:53 +0530 Subject: [PATCH 4/8] Update PascalTriangle.java --- src/main/java/com/thealgorithms/maths/PascalTriangle.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/PascalTriangle.java b/src/main/java/com/thealgorithms/maths/PascalTriangle.java index 823f5d35928a..62ee3bc9aa19 100644 --- a/src/main/java/com/thealgorithms/maths/PascalTriangle.java +++ b/src/main/java/com/thealgorithms/maths/PascalTriangle.java @@ -33,12 +33,6 @@ public class PascalTriangle { * */ - public static void main (String[] args) { - Scanner sc = new Scanner(System.in); - int n = sc.nextInt(); - printPascal(n); - } - public static int[][] pascal(int n) { /** From 4f420f92cc9744231f78ce30a6eb2411357ca34d Mon Sep 17 00:00:00 2001 From: Hardik Soni <79035082+iamhardikat11@users.noreply.github.com> Date: Sat, 11 Dec 2021 23:52:54 +0530 Subject: [PATCH 5/8] Create PascalTriangleTest.java --- .../maths/PascalTriangleTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/test/java/com/thealgorithms/maths/PascalTriangleTest.java diff --git a/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java new file mode 100644 index 000000000000..160051c8d08c --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java @@ -0,0 +1,39 @@ + +package com.thealgorithms.maths; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayLeftRotationTest { + + @Test + void testForOne() { + int[][] result = PascalTriangle.pascal(1); + int[][] expected = {{1}}; + assertArrayEquals(result,expected); + } + + @Test + void testForTwo() { + int[][] result = PascalTriangle.pascal(2); + int[][] expected = {{1,0},{1,1}}; + assertArrayEquals(result,expected); + } + + @Test + void testForFive() { + int[][] result = PascalTriangle.pascal(5); + int[][] expected = {{1,0,0,0,0},{1,1,0,0,0},{1,2,1,0,0},{1,3,3,1,0},{1,4,6,4,1}}; + assertArrayEquals(result,expected); + } + + @Test + void testForEight() { + int[][] result = PascalTriangle.pascal(1); + int[][] expected = {{1,0,0,0,0,0,0,0},{1,1,0,0,0,0,0,0},{1,2,1,0,0,0,0,0},{1,3,3,1,0,0,0,0},{1,4,6,4,1,0,0,0},{1,5,10,10,5,1,0,0},{1,6,15,20,15,6,1,0},{1,7,21,35,35,21,7,1}}; + assertArrayEquals(expected, result); + } + + +} From 6cdc4c61bf110ef409aa23c95d795c4a2ab6497c Mon Sep 17 00:00:00 2001 From: Hardik Soni <79035082+iamhardikat11@users.noreply.github.com> Date: Sat, 11 Dec 2021 23:53:37 +0530 Subject: [PATCH 6/8] Update PascalTriangleTest.java --- .../thealgorithms/maths/PascalTriangleTest.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java index 160051c8d08c..5b2d3c49594c 100644 --- a/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java +++ b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java @@ -8,30 +8,33 @@ class ArrayLeftRotationTest { @Test - void testForOne() { + void testForOne() + { int[][] result = PascalTriangle.pascal(1); - int[][] expected = {{1}}; + int[][] expected = {{1}}; assertArrayEquals(result,expected); } @Test - void testForTwo() { + void testForTwo() + { int[][] result = PascalTriangle.pascal(2); - int[][] expected = {{1,0},{1,1}}; + int[][] expected = {{1,0},{1,1}}; assertArrayEquals(result,expected); } @Test - void testForFive() { + void testForFive() + { int[][] result = PascalTriangle.pascal(5); - int[][] expected = {{1,0,0,0,0},{1,1,0,0,0},{1,2,1,0,0},{1,3,3,1,0},{1,4,6,4,1}}; + int[][] expected = {{1,0,0,0,0},{1,1,0,0,0},{1,2,1,0,0},{1,3,3,1,0},{1,4,6,4,1}}; assertArrayEquals(result,expected); } @Test void testForEight() { int[][] result = PascalTriangle.pascal(1); - int[][] expected = {{1,0,0,0,0,0,0,0},{1,1,0,0,0,0,0,0},{1,2,1,0,0,0,0,0},{1,3,3,1,0,0,0,0},{1,4,6,4,1,0,0,0},{1,5,10,10,5,1,0,0},{1,6,15,20,15,6,1,0},{1,7,21,35,35,21,7,1}}; + int[][] expected = {{1,0,0,0,0,0,0,0},{1,1,0,0,0,0,0,0},{1,2,1,0,0,0,0,0},{1,3,3,1,0,0,0,0},{1,4,6,4,1,0,0,0},{1,5,10,10,5,1,0,0},{1,6,15,20,15,6,1,0},{1,7,21,35,35,21,7,1}}; assertArrayEquals(expected, result); } From 0012b7a329b663871f5e1dfcc66c16cfd9d0f17f Mon Sep 17 00:00:00 2001 From: iamhardikat11 Date: Sun, 12 Dec 2021 23:27:46 +0530 Subject: [PATCH 7/8] Commit --- .../com/thealgorithms/maths/PascalTriangleTest.java | 2 +- .../thealgorithms/others/ArrayLeftRotationTest.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java index 5b2d3c49594c..4de0b321badb 100644 --- a/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java +++ b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java @@ -5,7 +5,7 @@ import static org.junit.jupiter.api.Assertions.*; -class ArrayLeftRotationTest { +class PascalTriangleTest { @Test void testForOne() diff --git a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java index 2efebb08f2ba..773a5aabf5dc 100644 --- a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java @@ -12,21 +12,21 @@ void testForOneElement() { int[] result = ArrayLeftRotation.rotateLeft(arr, 3); assertArrayEquals(arr, result); } - + @Test void testForZeroStep() { int[] arr = {3, 1, 5, 8, 6}; int[] result = ArrayLeftRotation.rotateLeft(arr, 0); assertArrayEquals(arr, result); } - + @Test void testForEqualSizeStep() { int[] arr = {3, 1, 5, 8, 6}; int[] result = ArrayLeftRotation.rotateLeft(arr, 5); assertArrayEquals(arr, result); } - + @Test void testForLowerSizeStep() { int[] arr = {3, 1, 5, 8, 6}; @@ -35,7 +35,7 @@ void testForLowerSizeStep() { int[] result = ArrayLeftRotation.rotateLeft(arr, n); assertArrayEquals(expected, result); } - + @Test void testForHigherSizeStep() { int[] arr = {3, 1, 5, 8, 6}; @@ -45,4 +45,4 @@ void testForHigherSizeStep() { assertArrayEquals(expected, result); } -} +} \ No newline at end of file From f45c226c6e551eef2bc369ed227daa5ce87709c3 Mon Sep 17 00:00:00 2001 From: iamhardikat11 Date: Sun, 12 Dec 2021 23:32:04 +0530 Subject: [PATCH 8/8] Commit --- .../com/thealgorithms/maths/PascalTriangleTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java index 4de0b321badb..93c87bd25926 100644 --- a/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java +++ b/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java @@ -11,7 +11,7 @@ class PascalTriangleTest { void testForOne() { int[][] result = PascalTriangle.pascal(1); - int[][] expected = {{1}}; + int[][] expected = {{1}}; assertArrayEquals(result,expected); } @@ -19,7 +19,7 @@ void testForOne() void testForTwo() { int[][] result = PascalTriangle.pascal(2); - int[][] expected = {{1,0},{1,1}}; + int[][] expected = {{1,0},{1,1}}; assertArrayEquals(result,expected); } @@ -27,14 +27,14 @@ void testForTwo() void testForFive() { int[][] result = PascalTriangle.pascal(5); - int[][] expected = {{1,0,0,0,0},{1,1,0,0,0},{1,2,1,0,0},{1,3,3,1,0},{1,4,6,4,1}}; + int[][] expected = {{1,0,0,0,0},{1,1,0,0,0},{1,2,1,0,0},{1,3,3,1,0},{1,4,6,4,1}}; assertArrayEquals(result,expected); } @Test void testForEight() { - int[][] result = PascalTriangle.pascal(1); - int[][] expected = {{1,0,0,0,0,0,0,0},{1,1,0,0,0,0,0,0},{1,2,1,0,0,0,0,0},{1,3,3,1,0,0,0,0},{1,4,6,4,1,0,0,0},{1,5,10,10,5,1,0,0},{1,6,15,20,15,6,1,0},{1,7,21,35,35,21,7,1}}; + int[][] result = PascalTriangle.pascal(8); + int[][] expected = {{1,0,0,0,0,0,0,0},{1,1,0,0,0,0,0,0},{1,2,1,0,0,0,0,0},{1,3,3,1,0,0,0,0},{1,4,6,4,1,0,0,0},{1,5,10,10,5,1,0,0},{1,6,15,20,15,6,1,0},{1,7,21,35,35,21,7,1}}; assertArrayEquals(expected, result); }