Skip to content

Commit 58d634d

Browse files
committed
refactoring. Refactor documentation in DFSmethods.class and rename it to SlicingMethods
1 parent ddbe18a commit 58d634d

File tree

10 files changed

+623
-65
lines changed

10 files changed

+623
-65
lines changed

QA/performance testing v0.01 01.02.2017/medium.in exeption.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Caused by: java.lang.OutOfMemoryError: Java heap space
2828
at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:421)
2929
at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:383)
3030
at ch.qos.logback.classic.Logger.info(Logger.java:579)
31-
at com.google.hashcode.utils.DFSMethods.getAvailableSteps(DFSMethods.java:40)
31+
at com.google.hashcode.utils.SlicingMethods.getAvailableSteps(DFSMethods.java:40)
3232
at com.google.hashcode.App.slicePizza(App.java:38)
3333
at com.google.hashcode.App.main(App.java:26)
3434
... 6 more

hs_err_pid9247.log

Lines changed: 554 additions & 0 deletions
Large diffs are not rendered by default.

outputDataSet/example.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

pizzaSlicing.log

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2017-02-09 21:18:28 [main] INFO com.google.hashcode.App - inputDataSets/example.in execution time: 165ms
2+
2017-02-09 21:18:28 [main] INFO com.google.hashcode.App - inputDataSets/small.in execution time: 25ms
3+
2017-02-09 21:19:10 [main] INFO com.google.hashcode.App - inputDataSets/medium.in execution time: 42063ms

src/main/java/com/google/hashcode/App.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.google.hashcode.entity.Pizza;
44
import com.google.hashcode.entity.Slice;
55
import com.google.hashcode.entity.Step;
6-
import com.google.hashcode.utils.DFSMethods;
6+
import com.google.hashcode.utils.SlicingMethods;
77
import com.google.hashcode.utils.IoUtils;
88
import com.google.hashcode.utils.Profiler;
99
import org.slf4j.Logger;
@@ -35,14 +35,14 @@ public static void slicePizza(String inputFile, String outputFile) throws IOExce
3535
List<Slice> output = new ArrayList<>();
3636
Pizza pizza = new Pizza(new File(inputFile), IoUtils.parsePizza(inputFile), IoUtils.parseSliceInstructions(inputFile));
3737
//get start positions
38-
startPositions = DFSMethods.cutAllStartPositions(pizza);
38+
startPositions = SlicingMethods.cutAllStartPositions(pizza);
3939
//get All steps
40-
Map<Slice, List<Step>> availableSteps = DFSMethods.getAvailableSteps(pizza, startPositions, output);
40+
Map<Slice, List<Step>> availableSteps = SlicingMethods.getAvailableSteps(pizza, startPositions, output);
4141
while (!availableSteps.values().stream().allMatch(List::isEmpty)) {
42-
Step step = DFSMethods.selectStep(availableSteps);
43-
DFSMethods.performStep(pizza, step, startPositions, output);
42+
Step step = SlicingMethods.selectStep(availableSteps);
43+
SlicingMethods.performStep(pizza, step, startPositions, output);
4444
//TODO available steps should include merging slices to each other
45-
availableSteps = DFSMethods.getAvailableSteps(pizza, startPositions, output);
45+
availableSteps = SlicingMethods.getAvailableSteps(pizza, startPositions, output);
4646
LOGGER.info("OUTPUT AFTER A STEP: "
4747
+ "\n " + output);
4848
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Optional<Cell> getCell(int y, int x) {
7272
public String toString() {
7373
StringBuilder stringBuilder = new StringBuilder();
7474
stringBuilder.append("slice : \n");
75-
if (maxX()+maxY() < 20) { //output coordinates
75+
if (maxX() + maxY() < 20) { //output coordinates
7676
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();
7777
int rowsCount = cells.stream().max(Comparator.comparingInt(Cell::getY)).get().getY();
7878
//output columns coordinates

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static List<Cell> parsePizza(String file) throws IOException {
4444
while ((fileLine = br.readLine()) != null) {
4545
for (int column = 0; column < fileLine.length(); column++) {
4646
Character literal = fileLine.charAt(column);
47-
counter ++;
47+
counter++;
4848
System.out.println("letter " + literal + " counter = " + counter);
4949
if (literal.toString().equals(Ingredient.TOMATO.toString())) {
5050
cells.add(new Cell(row, column, Ingredient.TOMATO));

src/main/java/com/google/hashcode/utils/DFSMethods.java renamed to src/main/java/com/google/hashcode/utils/SlicingMethods.java

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@
77
import java.util.*;
88
import java.util.stream.Collectors;
99

10-
public abstract class DFSMethods {
11-
private static final Logger LOGGER = LoggerFactory.getLogger(DFSMethods.class);
10+
public abstract class SlicingMethods {
11+
private static final Logger LOGGER = LoggerFactory.getLogger(SlicingMethods.class);
1212

13-
private DFSMethods() {
13+
private SlicingMethods() {
1414
}
1515

1616
/**
17-
* For each slice find all available steps.<br>
18-
* If founded start position that haven't any steps and it is unvalid ->
19-
* remove this slice from startPositions and add all it's cells to pizza.
17+
* Find all available steps for a given start positions, considering:<br>
18+
* * slice instructions for a given pizza<br>
19+
* * start position + slices should form a rectangle<br>
20+
* <p>
21+
* If there is no steps fo a given start position:<br>
22+
* 1. A start position is valid as a slice and can be cutted -> move it to output slices<br>
23+
* 2. A start position ISN'T valid as a slice -> remove it from startPositions and add all it cells back to a pizza
24+
*
2025
* @param pizza given pizza
21-
* @param startPositions given slices in the pizza
22-
* @param output
26+
* @param startPositions given start positions in the pizza(a slice with cells number 1..max slice cells number)
27+
* @param output list of valid and cutted slices
2328
* @return available steps
2429
*/
2530
public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice> startPositions, List<Slice> output) {
26-
Map<Slice, List<Step>> groupedSteps = new HashMap<>();
27-
Iterator iter = startPositions.iterator();
28-
while (iter.hasNext()) {
29-
Slice startPosition = (Slice) iter.next();
31+
Map<Slice, List<Step>> groupedByAStartPositionSteps = new HashMap<>();
32+
Iterator iterator = startPositions.iterator();
33+
while (iterator.hasNext()) {
34+
Slice startPosition = (Slice) iterator.next();
3035

3136
List<Step> steps = new ArrayList<>();
3237
Step stepLeft = startPosition.generateStepLeft(pizza);
@@ -38,71 +43,69 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
3843
steps.add(stepLeft);
3944
steps.add(stepBelow);
4045
steps.add(stepAbove);
41-
steps = steps.stream().filter(Objects::nonNull).collect(Collectors.toList());
42-
43-
if (steps.size() == 0) {
46+
steps = steps.stream()
47+
.filter(Objects::nonNull)
48+
.collect(Collectors.toList());
49+
LOGGER.debug("There is no steps fo a given start position !");
50+
if (steps.isEmpty()) {
51+
LOGGER.debug("A start position is valid as a slice and can be cutted ->" +
52+
" move it to output slices");
4453
if (startPosition.isValid(pizza)) {
45-
// if slice is valid and have'nt any steps -> cut it from
46-
// startPositions
4754
output.add(startPosition);
48-
iter.remove();
55+
iterator.remove();
4956
} else {
50-
// if slice isn't valid and have'nt any steps -> return all
51-
// it cells to pizza
57+
LOGGER.warn("A start position ISN'T valid as a slice -> " +
58+
"remove it from startPositions and add all it cells");
5259
pizza.getCells().addAll(startPosition.cells);
53-
iter.remove();
60+
iterator.remove();
5461
}
5562
} else {
56-
groupedSteps.put(startPosition, steps);
63+
groupedByAStartPositionSteps.put(startPosition, steps);
5764
}
5865
}
5966
LOGGER.info("available steps for" +
6067
"\npizza: " + pizza
61-
+ "\nsteps: " + groupedSteps);
62-
return groupedSteps;
68+
+ "\nsteps: " + groupedByAStartPositionSteps);
69+
return groupedByAStartPositionSteps;
6370
}
6471

6572
/**
66-
* Pick-ups a step with a minimal cells delta number,
67-
* execute it(cut it from the pizza, and add to a slice)
73+
* Performs a step with a minimal cells delta number and executes it (cut it from a pizza, and add to a slice)
6874
*
69-
* @param pizza given pizza
70-
* @param step step to perform
71-
* @param startPositions
72-
*@param output @return formed slice that includes an original slice and delta from a step
75+
* @param pizza given pizza
76+
* @param step step to perform
77+
* @param startPositions given start positions in the pizza(a slice with cells number 1..max slice cells number)
78+
* @param output list of valid and cutted slices
7379
*/
7480
public static void performStep(Pizza pizza, Step step, List<Slice> startPositions, List<Slice> output) {
7581
//1. Pick ups a steps list with minimal total cells number
76-
LOGGER.info("STEP TO PERFORM " + step);
77-
//2. Cut all the step delta cells from pizza
78-
LOGGER.info("pizza before step: " + pizza
82+
LOGGER.debug("STEP TO PERFORM " + step);
83+
//2. Cut all the step delta cells from a pizza
84+
LOGGER.debug("pizza before step: " + pizza
7985
+ "\ndelta to remove from the pizza: " + step.delta);
8086
pizza.getCells().removeAll(step.delta.cells);
81-
8287
//3. remove previous version start position from startPositions
8388
startPositions.remove(step.startPosition);
84-
8589
List<Cell> returnedList = step.startPosition.cells;
8690
returnedList.addAll(step.delta.cells);
8791
Slice finalSlice = new Slice(returnedList);
88-
89-
LOGGER.info("PIZZA AFTER STEP:" + pizza);
92+
LOGGER.debug("PIZZA AFTER STEP:" + pizza);
9093
//3. Add the step cells to an output slice
91-
92-
if(finalSlice.cells.size() == pizza.getSliceInstruction().getMaxNumberOfCellsPerSlice()){
94+
if (finalSlice.cells.size() == pizza.getSliceInstruction().getMaxNumberOfCellsPerSlice()) {
9395
output.add(finalSlice);
94-
} else{
96+
} else {
9597
startPositions.add(finalSlice);
9698
}
9799
}
98100

99101
/**
100102
* Selects a step which start position has minimal delta in all the steps
101103
*
102-
* @param steps
103-
* @return
104+
* @param steps available steps
105+
* @return a step with minimal delta
104106
*/
105107
public static Step selectStep(Map<Slice, List<Step>> steps) {
108+
//TODO test and refactor this peace of shit properly !!
106109
List<Step> min = steps.values().stream()
107110
.min(Comparator.comparingLong(value -> value.stream().map(step -> step.delta.cells.size()).count())).get();
108111
if (!min.isEmpty()) {
@@ -119,13 +122,15 @@ public static Step selectStep(Map<Slice, List<Step>> steps) {
119122
}
120123

121124
/**
122-
* Finds a cells type with minimal cells numbers and generates one cell slices from them
123-
* Delete the slices from the pizza
125+
* * Finds a cell type(tomato or mushroom) with minimal cells numbers<br>
126+
* * Generates a list of one cell slices from them<br>
127+
* * Deletes the slices from the pizza<br>
124128
*
125129
* @param pizza given pizza
126-
* @return slices that are start positions for future slicing
130+
* @return slices that are start positions for future slicing process
127131
*/
128132
public static List<Slice> cutAllStartPositions(Pizza pizza) {
133+
//1.Finds a cell type(tomato or mushroom) with minimal cells numbers
129134
List<Cell> mushrooms = pizza.getCells().stream()
130135
.filter(cell -> cell.ingredient.equals(Ingredient.MUSHROOM))
131136
.collect(Collectors.toList());
@@ -154,7 +159,7 @@ public static List<Slice> cutAllStartPositions(Pizza pizza) {
154159
.collect(Collectors.toList());
155160
pizza.getCells().removeAll(cellsToRemove);
156161
}
157-
LOGGER.info("pizza without start positions:"
162+
LOGGER.debug("pizza with removed start positions:"
158163
+ "\n" + pizza);
159164
return startPositions;
160165
}

src/test/java/com/google/hashcode/AppTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class AppTest {
88

99
@Test
1010
public void main() throws IOException {
11-
// App.main(new String[0]);
11+
// App.main(new String[0]);
1212
}
1313

1414
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void setup() throws IOException {
2929
@Test
3030
public void getAvailableSteps() throws IOException {
3131
List<Slice> output = new ArrayList<>();
32-
Map<Slice, List<Step>> actualMap = DFSMethods.getAvailableSteps(pizza, DFSMethods.cutAllStartPositions(pizza), output);
32+
Map<Slice, List<Step>> actualMap = SlicingMethods.getAvailableSteps(pizza, SlicingMethods.cutAllStartPositions(pizza), output);
3333
assertEquals(3, actualMap.keySet().size());
3434
assertEquals(3, actualMap.get(new Slice(new Cell(1, 1, Ingredient.MUSHROOM))).size());
3535
assertEquals(2, actualMap.get(new Slice(new Cell(1, 2, Ingredient.MUSHROOM))).size());
@@ -43,16 +43,16 @@ public void cutAllStartPositions() throws IOException {
4343
new Slice(new Cell(1, 2, Ingredient.MUSHROOM)),
4444
new Slice(new Cell(1, 3, Ingredient.MUSHROOM))
4545
);
46-
assertEquals(expected, DFSMethods.cutAllStartPositions(pizza));
46+
assertEquals(expected, SlicingMethods.cutAllStartPositions(pizza));
4747
assertEquals("We expect pizza size reduced to 15-3=12", 12, pizza.getCells().size());
4848
}
4949

5050
@Test
5151
public void performStep() {
52-
List<Slice> startPositions = DFSMethods.cutAllStartPositions(pizza);
52+
List<Slice> startPositions = SlicingMethods.cutAllStartPositions(pizza);
5353
List<Slice> output = new ArrayList<>();
54-
Map<Slice, List<Step>> availableSteps = DFSMethods.getAvailableSteps(pizza, startPositions, output);
55-
DFSMethods.performStep(pizza, DFSMethods.selectStep(availableSteps), startPositions, output);
54+
Map<Slice, List<Step>> availableSteps = SlicingMethods.getAvailableSteps(pizza, startPositions, output);
55+
SlicingMethods.performStep(pizza, SlicingMethods.selectStep(availableSteps), startPositions, output);
5656
assertEquals(new Slice(Arrays.asList(new Cell(1, 2, Ingredient.MUSHROOM), new Cell(2, 2, Ingredient.TOMATO)))
5757
, startPositions.get(2));
5858
assertEquals(11, pizza.getCells().size());

0 commit comments

Comments
 (0)