Skip to content

Commit

Permalink
Implementação do caso de teste IMPOSSIBLE.
Browse files Browse the repository at this point in the history
  • Loading branch information
wegneto committed Apr 21, 2015
1 parent 4a65242 commit 1783f41
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
31 changes: 23 additions & 8 deletions codejam/src/main/java/codejam/Milkshakes.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,37 @@ public class Milkshakes {

public String getBatches(int numFlavors, int numCustomers, int[][] customerFlavors) {
int[] result = new int[numFlavors];
for (int i = 0; i < result.length; i++) {
result[i] = -1;
}

boolean possible = true;

for (int customer = 0; customer < numCustomers; customer++) {
for (int flavor = 0; flavor < numFlavors; flavor++) {
for (int i = 0; i < customerFlavors[customer].length; i = i + 2) {
if (customerFlavors[customer][i] == (flavor + 1)) {
result[flavor] = customerFlavors[customer][i + 1];
}
for (int cf = 0; cf < customerFlavors[customer].length; cf++) {
int flavor = customerFlavors[customer][cf] - 1;
int malted = customerFlavors[customer][++cf];

if (result[flavor] != -1 && result[flavor] != malted) {
possible = false;
}

result[flavor] = malted;
}
}

StringBuilder output = new StringBuilder();
for (int i = 0; i < result.length; i++) {
output.append(result[i]).append(" ");
output.append("Case #1: ");
if (possible) {
for (int i = 0; i < result.length; i++) {
output.append(result[i] != -1 ? result[i] : 0).append(" ");
}

} else {
output.append("IMPOSSIBLE");
}

return "Case #1: " + output.toString();
return output.toString();
}

}
8 changes: 7 additions & 1 deletion codejam/src/test/java/codejam/MilkshakesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ public void getBatchesForTwoClientsThatLikeThreeFlavorsBetweenFiveFlavors() {
String output = (new Milkshakes()).getBatches(5, 2, new int[][] { { 1, 0 }, { 2, 0, 4, 0 } });
assertEquals("Case #1: 0 0 0 0 0 ", output);
}

@Test
public void getBatchesForTwoClientsThatLikeFourFlavorsOneMaltedBetweenFiveFlavors() {
String output = (new Milkshakes()).getBatches(5, 2, new int[][] { { 1, 0, 3, 1 }, { 2, 0, 4, 0 } });
assertEquals("Case #1: 0 0 1 0 0 ", output);
}

@Test
public void getBatchesForTwoClientsOneFlavorOneMalted() {
String output = (new Milkshakes()).getBatches(1 , 2, new int[][] { { 1, 1 }, { 1, 0 } });
assertEquals("Case #1: IMPOSSIBLE", output);
}

}

0 comments on commit 1783f41

Please sign in to comment.