Skip to content

Commit 2a4a3d8

Browse files
committed
4 kyu Create a funnel
1 parent 6cd3b1c commit 2a4a3d8

File tree

2 files changed

+238
-0
lines changed

2 files changed

+238
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.codewars.chrisgw.algorithms.kyu_4;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* <h2>Create a funnel</h2>
7+
* <a href="https://www.codewars.com/kata/585b373ce08bae41b800006e">https://www.codewars.com/kata/585b373ce08bae41b800006e</a>
8+
*/
9+
public class CreateAFunnel {
10+
11+
private final char[][] funnel;
12+
13+
14+
public CreateAFunnel() {
15+
this(5);
16+
}
17+
18+
public CreateAFunnel(int height) {
19+
funnel = new char[height][];
20+
for (int row = 0; row < height(); row++) {
21+
funnel[row] = new char[row + 1];
22+
Arrays.fill(funnel[row], ' ');
23+
}
24+
}
25+
26+
27+
public void fill(Character... args) {
28+
for (char value : args) {
29+
fill(value);
30+
}
31+
}
32+
33+
public void fill(char value) {
34+
for (int row = 0; row < height(); row++) {
35+
char[] funnelRow = funnel[row];
36+
for (int column = 0; column < funnelRow.length; column++) {
37+
if (funnelRow[column] == ' ') {
38+
funnelRow[column] = value;
39+
return;
40+
}
41+
}
42+
}
43+
}
44+
45+
46+
public Character drip() {
47+
char drippedValue = funnel[0][0];
48+
fillPlace(0, 0);
49+
return drippedValue;
50+
}
51+
52+
private void fillPlace(int row, int column) {
53+
funnel[row][column] = ' ';
54+
System.out.println(this);
55+
int rowAbove = row + 1;
56+
int leftWeight = weight(rowAbove, column, column);
57+
int rightWeight = weight(rowAbove, column + 1, column + 1);
58+
if (rightWeight > 0 && leftWeight >= rightWeight) {
59+
funnel[row][column] = funnel[rowAbove][column];
60+
fillPlace(rowAbove, column);
61+
} else if (rightWeight > 0) {
62+
funnel[row][column] = funnel[rowAbove][column + 1];
63+
fillPlace(rowAbove, column + 1);
64+
}
65+
}
66+
67+
private int weight(int row, int from, int to) {
68+
if (row >= height()) {
69+
return 0;
70+
}
71+
int weight = 0;
72+
char[] funnelRow = funnel[row];
73+
for (int column = from; column <= to; column++) {
74+
char value = funnelRow[column];
75+
if (value != ' ') {
76+
weight++;
77+
}
78+
}
79+
weight += weight(row + 1, from, to + 1);
80+
return weight;
81+
}
82+
83+
84+
public int height() {
85+
return funnel.length;
86+
}
87+
88+
89+
@Override
90+
public String toString() {
91+
StringBuilder sb = new StringBuilder();
92+
for (int row = height() - 1; row >= 0; row--) {
93+
sb.append(" ".repeat(height() - row - 1));
94+
sb.append("\\");
95+
for (int column = 0; column < funnel[row].length; column++) {
96+
char value = funnel[row][column];
97+
sb.append(value).append(" ");
98+
}
99+
sb.deleteCharAt(sb.length() - 1);
100+
sb.append('/').append('\n');
101+
}
102+
sb.deleteCharAt(sb.length() - 1);
103+
return sb.toString();
104+
}
105+
106+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.codewars.chrisgw.algorithms.kyu_4;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
/**
8+
* <h2>Create a funnel</h2>
9+
* <a href="https://www.codewars.com/kata/585b373ce08bae41b800006e">https://www.codewars.com/kata/585b373ce08bae41b800006e</a>
10+
*/
11+
class CreateAFunnelTest {
12+
13+
String SETUP = "\n\n-----------------------------------\n";
14+
String TEARDOWN = "\n";
15+
16+
@Test
17+
public void basicTests() {
18+
19+
CreateAFunnel funnel = new CreateAFunnel();
20+
String now = "";
21+
Character dropped = null;
22+
23+
System.out.println(SETUP + "Creating an empty funnel" + TEARDOWN);
24+
25+
now = "\\ /\n" +
26+
" \\ /\n" +
27+
" \\ /\n" +
28+
" \\ /\n" +
29+
" \\ /";
30+
System.out.println("Funnel should be now:\n" + now);
31+
assertEquals(now, funnel.toString());
32+
33+
34+
System.out.println(SETUP + "Testing the fill method" + TEARDOWN);
35+
36+
funnel.fill('1', '2', '3');
37+
now = ("\\ /\n" +
38+
" \\ /\n" +
39+
" \\ /\n" +
40+
" \\2 3/\n" +
41+
" \\1/");
42+
43+
System.out.println("Funnel should be now:\n" + now);
44+
assertEquals(now, funnel.toString());
45+
46+
47+
System.out.println(SETUP + "Testing the drip method" + TEARDOWN);
48+
49+
dropped = funnel.drip();
50+
now = ("\\ /\n" +
51+
" \\ /\n" +
52+
" \\ /\n" +
53+
" \\ 3/\n" +
54+
" \\2/");
55+
56+
System.out.println("Funnel should be now:\n" + now);
57+
assertEquals(now, funnel.toString());
58+
assertEquals(Character.valueOf('1'), dropped, "The drip method should return the value at the bottom of the funnel");
59+
60+
61+
System.out.println(SETUP + "Testing the fill functions with gaps and excess" + TEARDOWN);
62+
63+
funnel.fill('4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H');
64+
now = ("\\C D E F G/\n" +
65+
" \\8 9 A B/\n" +
66+
" \\5 6 7/\n" +
67+
" \\4 3/\n" +
68+
" \\2/");
69+
70+
System.out.println("Funnel should be now:\n" + now);
71+
assertEquals(now, funnel.toString());
72+
}
73+
74+
75+
@Test
76+
public void exampleTests() {
77+
CreateAFunnel funnel = new CreateAFunnel();
78+
String now = "";
79+
Character dropped = null;
80+
81+
funnel.fill('1', '2', '3', '4', '5', '6', '7', '8', '9');
82+
now = ("\\ /\n" +
83+
" \\7 8 9 /\n" +
84+
" \\4 5 6/\n" +
85+
" \\2 3/\n" +
86+
" \\1/");
87+
88+
System.out.println("Funnel should be now:\n" + now);
89+
assertEquals(now, funnel.toString());
90+
91+
92+
System.out.println(SETUP + "Testing the drip method" + TEARDOWN);
93+
94+
dropped = funnel.drip();
95+
now = ("\\ /\n" +
96+
" \\ 8 9 /\n" +
97+
" \\7 5 6/\n" +
98+
" \\4 3/\n" +
99+
" \\2/");
100+
101+
System.out.println("Funnel should be now:\n" + now);
102+
assertEquals(now, funnel.toString());
103+
assertEquals(Character.valueOf('1'), dropped, "The drip method should return the value at the bottom of the funnel");
104+
105+
106+
System.out.println(SETUP + "Testing the fill functions with gaps and excess" + TEARDOWN);
107+
108+
dropped = funnel.drip();
109+
now = "\\ /\n" +
110+
" \\ 9 /\n" +
111+
" \\7 8 6/\n" +
112+
" \\5 3/\n" +
113+
" \\4/";
114+
115+
System.out.println("Funnel should be now:\n" + now);
116+
assertEquals(now, funnel.toString());
117+
assertEquals(Character.valueOf('2'), dropped, "The drip method should return the value at the bottom of the funnel");
118+
119+
120+
dropped = funnel.drip();
121+
now = "\\ /\n" +
122+
" \\ /\n" +
123+
" \\7 9 6/\n" +
124+
" \\8 3/\n" +
125+
" \\5/";
126+
127+
System.out.println("Funnel should be now:\n" + now);
128+
assertEquals(now, funnel.toString());
129+
assertEquals(Character.valueOf('4'), dropped, "The drip method should return the value at the bottom of the funnel");
130+
}
131+
132+
}

0 commit comments

Comments
 (0)