-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathLadder.java
More file actions
64 lines (49 loc) · 1.82 KB
/
Ladder.java
File metadata and controls
64 lines (49 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package nextstep.ladder.domain;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Ladder {
private static final int BEGIN_INDEX = 0;
private final int verticalLineCount;
private final Lines lines;
public Ladder(int verticalLineCount, Lines lines) {
this.verticalLineCount = verticalLineCount;
this.lines = lines;
}
public Ladder(int verticalLineCount, int height) {
this(verticalLineCount, new Lines(generateLines(verticalLineCount, height)));
}
private static List<Line> generateLines(int verticalLineCount, int height) {
return Stream.generate(() -> new Line(verticalLineCount))
.limit(height)
.collect(Collectors.toList());
}
public Lines lines() {
return lines;
}
public String[][] result() {
List<Line> lines = this.lines.value();
int width = verticalLineCount * 2 - 1;
int height = this.lines.value().size();
String[][] ladder = new String[height][width];
IntStream.range(BEGIN_INDEX, height)
.forEach(i -> IntStream.range(BEGIN_INDEX, width)
.forEach(j -> {
if (isVerticalLine(j)) {
ladder[i][j] = "v";
return;
}
if (isHorizontalLine(lines, i, j)) {
ladder[i][j] = "h";
}
}));
return ladder;
}
private boolean isVerticalLine(int j) {
return j % 2 == 0;
}
private Boolean isHorizontalLine(List<Line> lines, int i, int j) {
return lines.get(i).value().get((j - 1) / 2);
}
}