Skip to content

Commit fe1f5bf

Browse files
committed
Merge remote-tracking branch 'remotes/origin/pizzaHumanReadableOutput' into DFSMethod_getAllAvailableSteps
2 parents 595dd8f + 8c43d97 commit fe1f5bf

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
lines changed

src/main/java/com/google/hashcode/entity/Cell.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ public int hashCode() {
4141
public int getX() {
4242
return x;
4343
}
44+
45+
public int getY() {
46+
return y;
47+
}
4448
}

src/main/java/com/google/hashcode/entity/Pizza.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.google.hashcode.entity;
22

33
import java.io.File;
4+
import java.util.Comparator;
45
import java.util.List;
56
import java.util.Optional;
67

@@ -51,13 +52,36 @@ public SliceInstruction getSliceInstruction() {
5152

5253
@Override
5354
public String toString() {
54-
return input.toString() +
55-
//TODO fix human readable output "\n" + IoUtils.convertToHumanReadableTable(cells) +
56-
"\n" + sliceInstruction.toString();
55+
return input.toString()
56+
+ ("\n" + sliceInstruction.toString()
57+
+ "\n" + outputCellsArray()).trim();
5758
}
58-
59+
5960
public boolean cotnainsAllCells(Slice slice){
6061
return slice.cells.stream().allMatch(cell->this.cells.contains(cell));
6162
}
62-
63+
64+
65+
private String outputCellsArray() {
66+
StringBuilder stringBuilder = new StringBuilder();
67+
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();
68+
int rowsCount = cells.stream().max(Comparator.comparingInt(Cell::getY)).get().getY();
69+
//output columns coordinates
70+
stringBuilder.append(" ");
71+
for (int column = 0; column < columnsCount + 1; column++) {
72+
stringBuilder.append(" ").append(column);
73+
}
74+
stringBuilder.append("\n");
75+
for (int row = 0; row < rowsCount + 1; row++) {
76+
//output rows coordinates
77+
stringBuilder.append(row).append(" ");
78+
for (int column = 0; column < columnsCount + 1; column++) {
79+
stringBuilder.append(this.getCell(row, column).toString()).append(" ");
80+
}
81+
stringBuilder.append("\n");
82+
}
83+
return stringBuilder.toString();
84+
}
85+
86+
6387
}

src/main/java/com/google/hashcode/utils/IoUtils.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,6 @@ public static SliceInstruction parseSliceInstructions(String file) throws IOExce
7272
}
7373
}
7474

75-
/**
76-
* Converts given pizza cells 2d array to human readable string representation
77-
*
78-
* @param ingredients given array
79-
* @return table like String representation
80-
*/
81-
public static String convertToHumanReadableTable(Cell[][] ingredients) {
82-
StringBuilder output = new StringBuilder();
83-
for (Cell[] row : ingredients) {
84-
for (Cell cell : row) {
85-
output.append(cell).append(" ");
86-
}
87-
output.append("\n");
88-
}
89-
return output.toString();
90-
//TODO reimplement for the pizza
91-
}
92-
9375
/**
9476
* Formats data from list of slices to the required output format
9577
*

src/test/java/com/google/hashcode/entity/PizzaTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.google.hashcode.entity;
22

3-
import com.google.hashcode.utils.InputFiles;
43
import com.google.hashcode.utils.IoUtils;
54
import org.junit.Test;
65

76
import java.io.File;
7+
import java.io.IOException;
88

9-
import static com.google.hashcode.utils.InputFiles.*;
9+
import static com.google.hashcode.utils.InputFiles.EXAMPLE_INPUT_FILE_PATH;
1010
import static org.junit.Assert.assertEquals;
1111

1212
/**
@@ -26,4 +26,16 @@ public void getCellException() throws Exception {
2626
pizza.getCell(100500, 0);
2727
}
2828

29+
@Test
30+
public void testToString() throws IOException {
31+
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
32+
System.out.println(pizza);
33+
assertEquals("inputDataSets/example.inSliceInstructions: \n" +
34+
"min 1 ingredient per slice, max 6 cells per slice \n" +
35+
" 0 1 2 3 4\n" +
36+
"0 T T T T T \n" +
37+
"1 T M M M T \n" +
38+
"2 T T T T T", pizza.toString());
39+
}
40+
2941
}

src/test/java/com/google/hashcode/utils/IoUtilsTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ private static List<Slice> createSlicesForParagonOutputExample() {
3333
slice0.cells.add(new Cell(0, 1, Ingredient.TOMATO));
3434
slice0.cells.add(new Cell(1, 1, Ingredient.MUSHROOM));
3535
slice0.cells.add(new Cell(2, 1, Ingredient.TOMATO));
36-
36+
3737
slice1.cells.add(new Cell(0, 2, Ingredient.TOMATO));
3838
slice1.cells.add(new Cell(1, 2, Ingredient.MUSHROOM));
3939
slice1.cells.add(new Cell(2, 2, Ingredient.TOMATO));
40-
40+
4141
slice2.cells.add(new Cell(0, 3, Ingredient.TOMATO));
4242
slice2.cells.add(new Cell(1, 3, Ingredient.MUSHROOM));
4343
slice2.cells.add(new Cell(2, 3, Ingredient.TOMATO));
@@ -50,11 +50,7 @@ private static List<Slice> createSlicesForParagonOutputExample() {
5050

5151
@Test
5252
public void parseExampleInput() throws IOException {
53-
/* List<Cell> ing = IoUtils.parsePizza(EXAMPLE_PIZZA_FILE);
54-
assertEquals("We expect" + EXAMPLE_PIZZA_FILE + "contains 3 rows", 3, ingredients.size);
55-
assertEquals("We expect" + EXAMPLE_PIZZA_FILE + "contains 5 columns", 5, ingredients[0].length);
56-
assertFalse("We expect no null value in ingredients", IoUtils.convertToHumanReadableTable(ingredients).contains("null"));
57-
*/
53+
List<Cell> input = IoUtils.parsePizza(EXAMPLE_PIZZA_FILE);
5854
}
5955

6056
@Test

0 commit comments

Comments
 (0)