create triangles with for loops - examples (#7513)
This commit is contained in:
parent
2a9050ef90
commit
7211b74b11
|
@ -30,6 +30,19 @@
|
|||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.8.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>pl.pragmatists</groupId>
|
||||
<artifactId>JUnitParams</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.algorithms.printtriangles;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class PrintTriangleExamples {
|
||||
|
||||
public static String printARightAngledTriangle(int N) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int r = 1; r <= N; r++) {
|
||||
for (int j = 1; j <= r; j++) {
|
||||
result.append("*");
|
||||
}
|
||||
result.append(System.lineSeparator());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static String printAnIsoscelesTriangle(int N) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int r = 1; r <= N; r++) {
|
||||
for (int sp = 1; sp <= N - r; sp++) {
|
||||
result.append(" ");
|
||||
}
|
||||
for (int c = 1; c <= (r * 2) - 1; c++) {
|
||||
result.append("*");
|
||||
}
|
||||
result.append(System.lineSeparator());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static String printAnIsoscelesTriangleUsingSubstring(int N) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
String helperString = StringUtils.repeat(' ', N - 1) + StringUtils.repeat('*', N * 2 - 1);
|
||||
|
||||
for (int r = 0; r < N; r++) {
|
||||
result.append(helperString.substring(r, N + 2 * r));
|
||||
result.append(System.lineSeparator());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(printARightAngledTriangle(5));
|
||||
System.out.println(printAnIsoscelesTriangle(5));
|
||||
System.out.println(printAnIsoscelesTriangleUsingSubstring(5));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package com.baeldung.algorithms.printtriangles;
|
||||
|
||||
import junitparams.JUnitParamsRunner;
|
||||
import junitparams.Parameters;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(JUnitParamsRunner.class)
|
||||
public class PrintTriangleExamplesUnitTest {
|
||||
|
||||
private static Object[][] rightAngledTriangles() {
|
||||
String expected0 = "";
|
||||
|
||||
String expected2 = "*" + System.lineSeparator()
|
||||
+ "**" + System.lineSeparator();
|
||||
|
||||
String expected5 = "*" + System.lineSeparator()
|
||||
+ "**" + System.lineSeparator()
|
||||
+ "***" + System.lineSeparator()
|
||||
+ "****" + System.lineSeparator()
|
||||
+ "*****" + System.lineSeparator();
|
||||
|
||||
String expected7 = "*" + System.lineSeparator()
|
||||
+ "**" + System.lineSeparator()
|
||||
+ "***" + System.lineSeparator()
|
||||
+ "****" + System.lineSeparator()
|
||||
+ "*****" + System.lineSeparator()
|
||||
+ "******" + System.lineSeparator()
|
||||
+ "*******" + System.lineSeparator();
|
||||
|
||||
return new Object[][] {
|
||||
{ 0, expected0 },
|
||||
{ 2, expected2 },
|
||||
{ 5, expected5 },
|
||||
{ 7, expected7 }
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
@Parameters(method = "rightAngledTriangles")
|
||||
public void whenPrintARightAngledTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
|
||||
String actual = PrintTriangleExamples.printARightAngledTriangle(nrOfRows);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
private static Object[][] isoscelesTriangles() {
|
||||
String expected0 = "";
|
||||
|
||||
String expected2 = " *" + System.lineSeparator()
|
||||
+ "***" + System.lineSeparator();
|
||||
|
||||
String expected5 = " *" + System.lineSeparator()
|
||||
+ " ***" + System.lineSeparator()
|
||||
+ " *****" + System.lineSeparator()
|
||||
+ " *******" + System.lineSeparator()
|
||||
+ "*********" + System.lineSeparator();
|
||||
|
||||
String expected7 = " *" + System.lineSeparator()
|
||||
+ " ***" + System.lineSeparator()
|
||||
+ " *****" + System.lineSeparator()
|
||||
+ " *******" + System.lineSeparator()
|
||||
+ " *********" + System.lineSeparator()
|
||||
+ " ***********" + System.lineSeparator()
|
||||
+ "*************" + System.lineSeparator();
|
||||
|
||||
return new Object[][] {
|
||||
{ 0, expected0 },
|
||||
{ 2, expected2 },
|
||||
{ 5, expected5 },
|
||||
{ 7, expected7 }
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
@Parameters(method = "isoscelesTriangles")
|
||||
public void whenPrintAnIsoscelesTriangleIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
|
||||
String actual = PrintTriangleExamples.printAnIsoscelesTriangle(nrOfRows);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Parameters(method = "isoscelesTriangles")
|
||||
public void whenPrintAnIsoscelesTriangleUsingSubstringIsCalled_ThenTheCorrectStringIsReturned(int nrOfRows, String expected) {
|
||||
String actual = PrintTriangleExamples.printAnIsoscelesTriangleUsingSubstring(nrOfRows);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue