Last active
April 2, 2016 18:51
-
-
Save thilko/88a33ad9e3a283ab42e3e6bf4ae638fb to your computer and use it in GitHub Desktop.
Diamond Kata
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 static java.util.Arrays.asList; | |
import org.hamcrest.MatcherAssert; | |
import static org.hamcrest.core.Is.is; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.StringJoiner; | |
import java.util.stream.Collector; | |
import java.util.stream.Collectors; | |
public class DiamondKataTest { | |
private static final String AVAILABLE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
@Test | |
public void numberForLetter() { | |
Assert.assertThat(numberForLetter("A"), is(1)); | |
Assert.assertThat(numberForLetter("B"), is(2)); | |
Assert.assertThat(numberForLetter("C"), is(3)); | |
} | |
@Test | |
public void spacesBeforeLetter() { | |
Assert.assertThat(spacesBeforeLetter("A"), is(asList(""))); | |
Assert.assertThat(spacesBeforeLetter("B"), is(asList(" ", ""))); | |
Assert.assertThat(spacesBeforeLetter("C"), is(asList(" ", " ", ""))); | |
} | |
@Test | |
public void spacesAfterLetter() { | |
Assert.assertThat(spacesAfterLetter("A"), is(asList(""))); | |
Assert.assertThat(spacesAfterLetter("B"), is(asList("", " "))); | |
Assert.assertThat(spacesAfterLetter("C"), is(asList("", " ", " "))); | |
} | |
@Test | |
public void letterForNumber() { | |
MatcherAssert.assertThat(letterForNumber(0), is("A")); | |
MatcherAssert.assertThat(letterForNumber(1), is("B")); | |
MatcherAssert.assertThat(letterForNumber(2), is("C")); | |
} | |
private String letterForNumber(int number) { | |
return AVAILABLE_LETTERS.substring(number, number + 1); | |
} | |
@Test | |
public void quadrant() { | |
Assert.assertThat(quadrant("A"), is(asList("A"))); | |
Assert.assertThat(quadrant("B"), is(asList(" A", "B "))); | |
Assert.assertThat(quadrant("C"), is(asList(" A", " B ", "C "))); | |
} | |
@Test | |
public void mirrorFromLeftToRight() { | |
Assert.assertThat(mirrorFromLeftToRight(quadrant("A")), is(asList("A"))); | |
Assert.assertThat(mirrorFromLeftToRight(quadrant("B")), is(asList(" A ", "B B"))); | |
Assert.assertThat(mirrorFromLeftToRight(quadrant("C")), is(asList(" A ", " B B ", "C C"))); | |
} | |
@Test | |
public void mirrorFromTopToBottom() { | |
Assert.assertThat(mirrorFromTopToBottom(quadrant("A")), is(asList("A"))); | |
Assert.assertThat(mirrorFromTopToBottom(quadrant("B")), is(asList(" A", "B ", " A"))); | |
Assert.assertThat(mirrorFromTopToBottom(quadrant("C")), is(asList(" A", " B ", "C ", " B ", " A"))); | |
} | |
@Test | |
public void printDiamond() { | |
Assert.assertThat(printDiamond("A"), is("A")); | |
Assert.assertThat(printDiamond("B"), is(" A \nB B\n A ")); | |
Assert.assertThat(printDiamond("C"), is(" A \n B B \nC C\n B B \n A ")); | |
} | |
private String printDiamond(String letter) { | |
List<String> diamond = mirrorFromTopToBottom(mirrorFromLeftToRight(quadrant(letter))); | |
return String.join("\n", diamond); | |
} | |
private List<String> mirrorFromTopToBottom(List<String> quadrant) { | |
ArrayList lines = new ArrayList(quadrant); | |
Collections.reverse(lines); | |
lines.remove(0); | |
ArrayList newList = new ArrayList(); | |
newList.addAll(quadrant); | |
newList.addAll(lines); | |
return newList; | |
} | |
private List<String> mirrorFromLeftToRight(List<String> quadrant) { | |
return quadrant.stream() | |
.map((input) -> input + new StringBuffer(input).reverse().deleteCharAt(0).toString()) | |
.collect(Collectors.toList()); | |
} | |
private List<String> quadrant(String letter) { | |
List<String> spacesBefore = spacesBeforeLetter(letter); | |
List<String> spacesAfter = spacesAfterLetter(letter); | |
List<String> quadrant = new ArrayList<>(); | |
for (int i = 0; i < numberForLetter(letter); i++) { | |
quadrant.add(spacesBefore.get(i) + letterForNumber(i) + spacesAfter.get(i)); | |
} | |
return quadrant; | |
} | |
private List<String> spacesAfterLetter(String letter) { | |
List<String> list = new ArrayList<>(spacesBeforeLetter(letter)); | |
Collections.reverse(list); | |
return list; | |
} | |
private List<String> spacesBeforeLetter(String letter) { | |
int number = numberForLetter(letter); | |
ArrayList<String> result = new ArrayList<String>(); | |
for (int i = number; i > 0; i--) { | |
result.add(String.join("", Collections.nCopies(i - 1, " "))); | |
//result.add(Stream.generate(()-> " ").limit(i-1).collect(Collectors.joining())); | |
} | |
return result; | |
} | |
private int numberForLetter(String letter) { | |
return AVAILABLE_LETTERS.indexOf(letter) + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment