refactor: FloydTriangle (#5367)

This commit is contained in:
Alex Klymenko 2024-08-23 14:55:13 +02:00 committed by GitHub
parent 34089774f3
commit ce4eb55e0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 64 additions and 10 deletions

View File

@ -1,22 +1,30 @@
package com.thealgorithms.others; package com.thealgorithms.others;
import java.util.Scanner; import java.util.ArrayList;
import java.util.List;
final class FloydTriangle { final class FloydTriangle {
private FloydTriangle() { private FloydTriangle() {
} }
public static void main(String[] args) { /**
Scanner sc = new Scanner(System.in); * Generates a Floyd Triangle with the specified number of rows.
System.out.println("Enter the number of rows which you want in your Floyd Triangle: "); *
int r = sc.nextInt(); * @param rows The number of rows in the triangle.
int n = 0; * @return A List representing the Floyd Triangle.
sc.close(); */
for (int i = 0; i < r; i++) { public static List<List<Integer>> generateFloydTriangle(int rows) {
List<List<Integer>> triangle = new ArrayList<>();
int number = 1;
for (int i = 0; i < rows; i++) {
List<Integer> row = new ArrayList<>();
for (int j = 0; j <= i; j++) { for (int j = 0; j <= i; j++) {
System.out.print(++n + " "); row.add(number++);
} }
System.out.println(); triangle.add(row);
} }
return triangle;
} }
} }

View File

@ -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<List<Integer>> 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<List<Integer>> expectedOutput = Arrays.asList(Arrays.asList(1));
assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(1));
}
@Test
public void testGenerateFloydTriangleWithZeroRows() {
List<List<Integer>> expectedOutput = Arrays.asList();
assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(0));
}
@Test
public void testGenerateFloydTriangleWithNegativeRows() {
List<List<Integer>> expectedOutput = Arrays.asList();
assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(-3));
}
@Test
public void testGenerateFloydTriangleWithMultipleRows() {
List<List<Integer>> 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<List<Integer>> 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));
}
}