From ce4eb55e0e7c5216e2cba53217627f6bbe8b5083 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Fri, 23 Aug 2024 14:55:13 +0200 Subject: [PATCH] refactor: `FloydTriangle` (#5367) --- .../thealgorithms/others/FloydTriangle.java | 28 +++++++---- .../others/FloydTriangleTest.java | 46 +++++++++++++++++++ 2 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/FloydTriangleTest.java diff --git a/src/main/java/com/thealgorithms/others/FloydTriangle.java b/src/main/java/com/thealgorithms/others/FloydTriangle.java index fbeaec33..dff48443 100644 --- a/src/main/java/com/thealgorithms/others/FloydTriangle.java +++ b/src/main/java/com/thealgorithms/others/FloydTriangle.java @@ -1,22 +1,30 @@ package com.thealgorithms.others; -import java.util.Scanner; +import java.util.ArrayList; +import java.util.List; final class FloydTriangle { private FloydTriangle() { } - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("Enter the number of rows which you want in your Floyd Triangle: "); - int r = sc.nextInt(); - int n = 0; - sc.close(); - for (int i = 0; i < r; i++) { + /** + * Generates a Floyd Triangle with the specified number of rows. + * + * @param rows The number of rows in the triangle. + * @return A List representing the Floyd Triangle. + */ + public static List> generateFloydTriangle(int rows) { + List> triangle = new ArrayList<>(); + int number = 1; + + for (int i = 0; i < rows; i++) { + List row = new ArrayList<>(); for (int j = 0; j <= i; j++) { - System.out.print(++n + " "); + row.add(number++); } - System.out.println(); + triangle.add(row); } + + return triangle; } } diff --git a/src/test/java/com/thealgorithms/others/FloydTriangleTest.java b/src/test/java/com/thealgorithms/others/FloydTriangleTest.java new file mode 100644 index 00000000..afa280c0 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/FloydTriangleTest.java @@ -0,0 +1,46 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + +public class FloydTriangleTest { + + @Test + public void testGenerateFloydTriangleWithValidInput() { + List> expectedOutput = Arrays.asList(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6)); + assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(3)); + } + + @Test + public void testGenerateFloydTriangleWithOneRow() { + List> expectedOutput = Arrays.asList(Arrays.asList(1)); + assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(1)); + } + + @Test + public void testGenerateFloydTriangleWithZeroRows() { + List> expectedOutput = Arrays.asList(); + assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(0)); + } + + @Test + public void testGenerateFloydTriangleWithNegativeRows() { + List> expectedOutput = Arrays.asList(); + assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(-3)); + } + + @Test + public void testGenerateFloydTriangleWithMultipleRows() { + List> expectedOutput = Arrays.asList(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9, 10), Arrays.asList(11, 12, 13, 14, 15)); + assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(5)); + } + + @Test + public void testGenerateFloydTriangleWithMoreMultipleRows() { + List> expectedOutput = Arrays.asList(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9, 10), Arrays.asList(11, 12, 13, 14, 15), Arrays.asList(16, 17, 18, 19, 20, 21), Arrays.asList(22, 23, 24, 25, 26, 27, 28)); + assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(7)); + } +}