Last active
August 29, 2015 14:20
-
-
Save up1/aa269419335f28923764 to your computer and use it in GitHub Desktop.
Coding Dojo :: Diamond Print
This file contains 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
Input :: A | |
OutPut | |
A | |
Input :: B | |
Output | |
A | |
B B | |
A | |
Input :: C | |
Output | |
A | |
B B | |
C C | |
B B | |
A | |
Input :: D | |
Output | |
A | |
B B | |
C C | |
D D | |
C C | |
B B | |
A | |
Input :: E | |
Output | |
A | |
B B | |
C C | |
D D | |
E E | |
D D | |
C C | |
B B | |
A | |
This file contains 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
==== DiamondTest.java ==== | |
@Test | |
public void A_should_only_one() { | |
Diamond p = new Diamond(); | |
assertEquals("A", p.create('A')); | |
} | |
==== Diamond.java ==== | |
public class Diamond { | |
public String create(char input) { | |
return "A"; | |
} | |
} |
This file contains 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
==== DiamondTest.java ==== | |
@Test | |
public void B_should_show_one_charecter() { | |
assertEquals("AB", p.create('B')); | |
} | |
==== Diamond.java ==== | |
public class Diamond { | |
public String create(char input) { | |
if(input == 'A'){ | |
return "A"; | |
} | |
return "AB"; | |
} | |
} |
This file contains 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
==== Diamond.java ==== | |
public class Diamond { | |
public String create(char input) { | |
String result = ""; | |
for(char start = 'A'; start<=input; start++) { | |
result += start; | |
} | |
return result; | |
} | |
} |
This file contains 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
==== DiamondTest.java ==== | |
@Test | |
public void B_should_repeat_of_second_line() { | |
assertEquals("ABB", p.create('B')); | |
} | |
==== Diamond.java ==== | |
public class Diamond { | |
public String create(char input) { | |
String result = ""; | |
for(char start = 'A'; start<=input; start++) { | |
result += start; | |
} | |
if(input != 'A') { | |
result += input; | |
} | |
return result; | |
} | |
} |
This file contains 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
==== DiamondTest.java ==== | |
@Test | |
public void B_should_repeat_of_second_line() { | |
assertEquals("ABBA", p.create('B')); | |
} | |
==== Diamond.java ==== | |
public class Diamond { | |
public String create(char input) { | |
String result = ""; | |
for(char start = 'A'; start<=input; start++) { | |
result += start; | |
} | |
if(input != 'A') { | |
result += "BA"; | |
} | |
return result; | |
} | |
} |
This file contains 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
==== DiamondTest.java ==== | |
@Test | |
public void A_should_only_one() { | |
assertEquals("A\n", p.create('A')); | |
} | |
@Test | |
public void B_should_add_newline_of_every_line() { | |
assertEquals("A\nBB\nA\n", p.create('B')); | |
} | |
==== Diamond.java ==== | |
public class Diamond { | |
public String create(char input) { | |
String result = ""; | |
String bottomLine = ""; | |
for(char start = 'A'; start<=input; start++) { | |
String line = ""+start; | |
if( start != 'A' ) { | |
line += input; | |
} | |
line += "\n"; | |
result += line; | |
if( start != input ) { | |
bottomLine = line + bottomLine; | |
} | |
} | |
return result + bottomLine; | |
} | |
} |
This file contains 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
==== Diamond.java ==== | |
public class Diamond { | |
public String create(char input) { | |
String result = ""; | |
String bottomLine = ""; | |
for(char start = 'A'; start<=input; start++) { | |
String line = buildLine(start, input); | |
result += line; | |
if( start != input ) { | |
bottomLine = line + bottomLine; | |
} | |
} | |
return result + bottomLine; | |
} | |
private String buildLine(char start, char input) { | |
String line = ""+ start; | |
if( start != 'A' ) { | |
line += input; | |
} | |
line += "\n"; | |
return line; | |
} | |
} |
This file contains 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
==== DiamondTest.java ==== | |
@Test | |
public void B_should_add_spacebar_of_first_line() { | |
assertEquals(" A\nB B\n A\n", p.create('B')); | |
} | |
==== Diamond.java ==== | |
private String buildLine(char start, char input) { | |
String line = ""; | |
if( start == 'A' ) { | |
line = " " + start; | |
} else { | |
line = start + " " + start; | |
} | |
line += "\n"; | |
return line; | |
} |
This file contains 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
==== Diamond.java ==== | |
private String buildLine(char start, char input) { | |
String line = ""; | |
line += addSpace(input-start); | |
line += start; | |
if(start != 'A') { | |
line += " " + start; | |
} | |
line += "\n"; | |
return line; | |
} | |
private String addSpace(int count) { | |
String result = ""; | |
for(int i=0; i<count; i++) { | |
result += " "; | |
} | |
return result; | |
} |
This file contains 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
==== DiamondTest.java ==== | |
@Test | |
public void C_should_show_diamond() { | |
String expectedResult = " A\n"+ | |
" B B\n"+ | |
"C C\n"+ | |
" B B\n"+ | |
" A\n"; | |
assertEquals(expectedResult, p.create('C')); | |
} |
This file contains 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
==== Diamond.java ==== | |
private String buildLine(char start, char input) { | |
String line = ""; | |
line += addSpace(input-start); | |
line += start; | |
line += addRestOfLine(start); | |
return line; | |
} | |
private String addSpace(int count) { | |
String result = ""; | |
for(int i=0; i<count; i++) { | |
result += " "; | |
} | |
return result; | |
} | |
private String addRestOfLine(char start) { | |
String line = ""; | |
if(start != 'A') { | |
int count = (start - 'A') * 2 - 1; | |
return addSpace(count) + start + "\n"; | |
} | |
return "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment