Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diamond kata NewDay interview Test -added 3 files #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Diamond Kata/Diamond.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//NewDay CodeDojos/Diamond Kata

package newday.test.diamond;

public class Diamond {
public static String create(char letter) {
if (letter < 'A' || letter > 'Z') {
return "Invalid input. Please provide a letter between A and Z.";
}

StringBuilder result = new StringBuilder();

// Print the top half of the diamond
result.append(createLine(letter, 'A'));
for (char current = 'B'; current <= letter; current++) {
result.append(createLine(letter, current));
}

// Print the bottom half of the diamond
for (char current = (char)(letter - 1); current >= 'A'; current--) {
result.append(createLine(letter, current));
}

return result.toString();
}

private static String createLine(char letter, char current) {
StringBuilder line = new StringBuilder();

// Leading spaces
for (char c = current; c < letter; c++) {
line.append(" ");
}

// Current character
line.append(current);

// Special handling for characters after 'A'
if (current != 'A') {
int spaces = (current - 'A') * 2 - 1;
for (int i = 0; i < spaces; i++) {
line.append(" ");
}
line.append(current);
}

// Newline
line.append("\n");

return line.toString();
}
}
56 changes: 56 additions & 0 deletions Diamond Kata/DiamondPrinterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package newday.test.diamond;



import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class DiamondPrinterTest {

@Test
public void testCreateDiamondA() {
assertEquals("A\n", Diamond.create('A'));
}

@Test
public void testCreateDiamondBCharacterSequence() {
assertEquals("AB\n", Diamond.create('B'));
}

@Test
public void testCreateDiamondBRepeatCharacters() {
assertEquals("ABB\n", Diamond.create('B'));
}

@Test
public void testCreateDiamondBSeparateLines() {
assertEquals("A\nBB\n", Diamond.create('B'));
}

@Test
public void testCreateDiamondBIndentation() {
assertEquals(" A\nB B\n A\n", Diamond.create('B'));
}

@Test
public void testCreateDiamondBInterCharacterSpaces() {
assertEquals(" A\nB B\n A\n", Diamond.create('B'));
}

@Test
public void testCreateDiamondBSymmetry() {
assertEquals(" A\nB B\n A\n", Diamond.create('B'));
}

@Test
public void testCreateDiamondC() {
assertEquals(" A\n B B\nC C\n B B\n A\n", Diamond.create('C'));
}

@Test
public void testCreateDiamondD() {
assertEquals(" A\n B B\n C C\nD D\n C C\n B B\n A\n", Diamond.create('D'));
}

// Add more test cases as needed to cover additional scenarios
}
16 changes: 16 additions & 0 deletions Diamond Kata/DiamondTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package newday.test.diamond;

public class DiamondTest {

public static void main(String[] args) {
// Test with different characters
System.out.println("Diamond for character 'A':");
System.out.println(Diamond.create('A'));

System.out.println("Diamond for character 'B':");
System.out.println(Diamond.create('B'));

System.out.println("Diamond for character 'C':");
System.out.println(Diamond.create('D'));
}
}