Last active
January 11, 2018 14:16
-
-
Save crevier/956033ee2261ada410ddeb30a0700c98 to your computer and use it in GitHub Desktop.
20180111_kata_lunch : https://www.hackerrank.com/challenges/staircase/problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class StairCaseSolution { | |
public static String staircase(int n) { | |
if (n <= 0) | |
return ""; | |
StringBuilder resultBuilder = new StringBuilder(); | |
for (int spaceNumber = n - 1; spaceNumber >= 0; spaceNumber--) { | |
resultBuilder.append(createLine(n, spaceNumber)).append('\n'); | |
} | |
resultBuilder.deleteCharAt(resultBuilder.length()-1); | |
return resultBuilder.toString(); | |
} | |
private static char[] createLine(int sharpNumber, int spaceNumber) { | |
char[] line = new char[sharpNumber]; | |
Arrays.fill(line, 0, spaceNumber, ' '); | |
Arrays.fill(line,spaceNumber, sharpNumber, '#'); | |
return line; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
public class StairCaseTest { | |
@Test | |
public void should_return_empty_string_when_given_0(){ | |
assertEquals("", StairCaseSolution.staircase(0)); | |
} | |
@Test | |
public void should_return_empty_string_when_given_minus10(){ | |
assertEquals("", StairCaseSolution.staircase(-10)); | |
} | |
@Test | |
public void should_return_sharp_when_given_1(){ | |
assertEquals("#", StairCaseSolution.staircase(1)); | |
} | |
@Test | |
public void should_return_01n11__when_given_2(){ | |
assertEquals(" #\n##", StairCaseSolution.staircase(2)); | |
} | |
@Test | |
public void should_return_001n011n111__when_given_3(){ | |
assertEquals(" #\n ##\n###", StairCaseSolution.staircase(3)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment