Skip to content

Instantly share code, notes, and snippets.

@jpshelley
Created April 6, 2015 20:27
Show Gist options
  • Save jpshelley/bc4740ee6a7e91508d9f to your computer and use it in GitHub Desktop.
Save jpshelley/bc4740ee6a7e91508d9f to your computer and use it in GitHub Desktop.
MakeChocalate-iFit
public class MakeChocolate {
/********************************
* We want make a package of goal kilos of chocolate. We have
* small bars (1 kilo each) and big bars (5 kilos each).
* Return the number of small bars to use, assuming we always
* use big bars before small bars. Return -1 if it can't be done.
*
* See the verify lines below for examples of input
* and expected output.
*
* Run this class, If you see errors, it is because the tests below
* did not pass. Once the tests do pass, you will see a log of `Success!`
********************************/
public static void main(String [] args) {
try {
// These are the tests that will run against your code
// vars = small, big, goal, expected answer
verify(4, 1, 9, 4);
verify(4, 1, 10, -1);
verify(4, 1, 7, 2);
verify(6, 2, 7, 2);
verify(4, 1, 5, 0);
verify(4, 1, 4, 4);
verify(5, 4, 9, 4);
verify(9, 3, 18, 3);
verify(3, 1, 9, -1);
verify(1, 2, 7, -1);
verify(1, 2, 6, 1);
verify(1, 2, 5, 0);
verify(6, 1, 10, 5);
verify(6, 1, 11, 6);
verify(6, 1, 12, -1);
verify(6, 1, 13, -1);
verify(6, 2, 10, 0);
verify(6, 2, 11, 1);
verify(6, 2, 12, 2);
verify(60, 100, 550, 50);
verify(1000, 1000000, 5000006, 6);
verify(7, 1, 12, 7);
verify(7, 1, 13, -1);
verify(7, 2, 13, 3);
System.out.println("Success!");
} catch (Error err) {
System.out.println(err.getMessage());
}
}
/********************************
* YOUR CODE BELOW HERE
********************************/
private static int make(int small, int big, int goal) {
int actualBig = big * 5;
if (goal - actualBig < 0) {
return make(small, big - 1, goal);
}
int leftover = goal - actualBig;
if (leftover <= small) {
return leftover;
} else {
return -1;
}
}
/********************************
* YOUR CODE ABOVE HERE
********************************/
private static void verify(int small, int big, int goal, int expected) {
int answer = make(small, big, goal);
if (answer != expected) {
throw new Error("wrong answer, expected " + answer + " to equal " + expected);
}
}
}
public class TripleUp {
/********************************
* Return true if the array contains, somewhere, three increasing
* adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25.
*
* See the verify lines below for examples of input
* and expected output.
*
* Run this class, If you see errors, it is because the tests below
* did not pass. Once the tests do pass, you will see a log of `Success!`
********************************/
public static void main(String [] args) {
try {
// These are the tests that will run against your code
// vars = array of ints, expected answer
verify(new int[]{1, 4, 5, 6, 2}, true);
verify(new int[]{1, 2, 4, 5, 7, 6, 5, 6, 7, 6}, true);
verify(new int[]{1, 2, 4, 5, 7, 6, 5, 7, 7, 6}, false);
verify(new int[]{1, 2}, false);
verify(new int[]{10, 9, 8, -100, -99, -98, 100}, true);
verify(new int[]{10, 9, 8, -100, -99, 99, 100}, false);
System.out.println("Success!");
} catch (Error err) {
System.out.println(err.getMessage());
}
}
/********************************
* YOUR CODE BELOW HERE
********************************/
private static boolean triple(int[] nums) {
if (nums.length >= 3){
if (nums[0] == (nums[1] - 1) && nums[0] == (nums[2] - 2)) {
return true;
} else if (nums.length >=4) {
int[] newArray = new int[nums.length - 1];
System.arraycopy(nums, 1, newArray, 0, newArray.length);
return triple(newArray);
} else {
return false;
}
} else {
return false;
}
}
/********************************
* YOUR CODE ABOVE HERE
********************************/
private static void verify(int[] nums, boolean expected) {
boolean answer = triple(nums);
if (answer != expected) {
throw new Error("wrong answer, expected " + answer + " to equal " + expected);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment