Skip to content

Instantly share code, notes, and snippets.

@benhopper1
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhopper1/c8cf431c81a719c62722 to your computer and use it in GitHub Desktop.
Save benhopper1/c8cf431c81a719c62722 to your computer and use it in GitHub Desktop.
Java Assessment (Back-end Developer)
package hopper.test.v001;
import java.util.Arrays;
class Main extends TestCase {
public static void main(String[] args){
//=======================================================================
//-- Problem 1
//=======================================================================
// -- Test 1
assertEqual(Multiples.sumOfRange(9, new int[] {3,5}), 23);
// -- Test 2
assertEqual(Multiples.sumOfRange(9, new int[] {5,3}), 23);
// -- Test 3
assertEqual(Multiples.sumOfRange(9, new int[] {5,3,0}), 23);
//=======================================================================
//-- Problem 2
//=======================================================================
// -- Test 1
assertEqual(ShoppingCart.getTotal(new int[]{1999}, 09.25f),"$21.84");
//=======================================================================
//-- Problem 3
//=======================================================================
// -- Test 1
assertEqual(StringUtility.stringToArray("The quick brown fox") , new String[]{"The", "quick", "brown", "fox"});
//=======================================================================
//-- Problem 4
//=======================================================================
// -- Test 1
assertEqual(StringUtility.fullNameToFormalFormat("First Middle0 Middle1 Last") , "Last, First Middle0");
// -- Test 2
assertEqual(StringUtility.fullNameToFormalFormat("") , "");
// -- Test 3
assertEqual(StringUtility.fullNameToFormalFormat("LastName") , "LastName");
// -- Test 4
assertEqual(StringUtility.fullNameToFormalFormat("Samuel L Jackson") , "Jackson, Samuel L");
// -- Test 5
assertEqual(StringUtility.fullNameToFormalFormat("Samuel Jackson") , "Jackson, Samuel");
//=======================================================================
//-- Problem 5
//=======================================================================
// -- Test 1
assertEqual(StringUtility.toFormatedDate(new int[]{2012, 3, 16}), "March 16, 2012");
// -- Test 2
assertEqual(StringUtility.toFormatedDate(new int[]{2012, 1, 16}), "January 16, 2012");
//=======================================================================
//-- Problem 6
//=======================================================================
// -- Test 1
assertEqual(PrimeFactor.getMax(56788), 14197);
// -- Test 2
assertEqual(PrimeFactor.getMax(3621), 71);
// -- Test 3
assertEqual(PrimeFactor.getMax(784912), 49057);
}
}
class TestCase{
static private void fail(){
System.out.println("Test Failed");
}
static private void pass(){
System.out.println("Test Passed");
}
static private void assertTrue(boolean condition){
if (!condition) {
fail();
} else {
pass();
}
}
static public void assertEqual(int actual, int expected){
assertTrue(actual == expected);
}
static public void assertEqual(String actual, String expected){
assertTrue(expected.equals(actual));
}
static public void assertEqual(int[] actual, int[] expected){
assertTrue(Arrays.equals(actual, expected));
}
static public void assertEqual(String[] actual, String[] expected){
assertTrue(Arrays.equals(actual, expected));
}
}
package hopper.test.v001;
/*
* Reference:
* http://math.stackexchange.com/questions/9259/find-the-sum-of-all-the-multiples-of-3-or-5-below-1000
*
*
* -- REQUIREMENT OF INPUT:
* Should be provided with unique array.
* I did not include guard...
*/
public class Multiples {
private Multiples(){}
static public int sumOfRange (int inRangeTop, int[] inValues){
int multiplesSum = 0;
for(int value : inValues){
int quotient = (int)((double)inRangeTop/value);
multiplesSum += (int)((value * quotient * (quotient + 1)) / 2);
}
int intersectionQuotient = (int)((double)inRangeTop/leastCommonMultiple(inValues));
int sumOfIntersectionMultiples = (leastCommonMultiple(inValues) * intersectionQuotient * (intersectionQuotient + 1)) / 2;
return multiplesSum - sumOfIntersectionMultiples;
}
static private int leastCommonMultiple(int inValueA, int inValueB ){
int i = 1;
while(true){
if(((inValueA * i) % inValueB) < 1)
return inValueA * i;
i++;
}
}
static private int leastCommonMultiple(int[] inValues){
if(inValues.length < 2)
return inValues[0];
int workRegister = inValues[0];
for(int i = 1; i < inValues.length; i++){
workRegister = leastCommonMultiple(inValues[i], workRegister);
}
return workRegister;
}
}
package hopper.test.v001;
/*
* Reference:
* http://stackoverflow.com/questions/23287/largest-prime-factor-of-a-number
*
*
*/
public class PrimeFactor {
private PrimeFactor(){}
static public int getMax(int inNumber){
int result = 1;
if(inNumber % 2 == 0){
result = 2;
while(inNumber % 2 == 0)
inNumber /= 2;
}
for(int i = 3; i < Math.sqrt(inNumber); i+= 2){
if(inNumber % i == 0){
result = i;
while (inNumber % i == 0)
inNumber /= i;
}
}
return Math.max(inNumber, result);
}
}
package hopper.test.v001;
/*
* Reference:
* http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial
* http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency
*
*/
import java.math.BigDecimal;
public class ShoppingCart {
private ShoppingCart(){}
static public String getTotal(int[] inPennyArray, float inTax){
BigDecimal dollarTally = new BigDecimal("0");
for(int thePennyAmount : inPennyArray){
dollarTally = dollarTally.add(new BigDecimal(thePennyAmount).multiply(new BigDecimal(".01")));
}
dollarTally = dollarTally.add(dollarTally.multiply(new BigDecimal(inTax).multiply(new BigDecimal(".01"))));
return String.format("$%s",dollarTally.setScale(2, BigDecimal.ROUND_HALF_EVEN));
}
}
package hopper.test.v001;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class StringUtility {
private StringUtility(){}
static public String[] stringToArray(String inString){
return inString.split(" ");
}
static public String fullNameToFormalFormat(String inString){
String[] stringArray = stringToArray(inString);
if(stringArray.length > 2){
return String.format("%s, %s %s", stringArray[stringArray.length - 1], stringArray[0], stringArray[1]);
}
if(stringArray.length == 2){
return String.format("%s, %s",stringArray[1], stringArray[0]);
}
return stringArray[0];
}
static public String toFormatedDate(int[] inDateParts){
//-- assume [0,1,2] == Y, M, D, : output month as 1 based(1:12)
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMMMMMMM dd, yyyy");
Calendar calendar = new GregorianCalendar(inDateParts[0], inDateParts[1] - 1, inDateParts[2]);
return simpleDateFormat.format((calendar.getTime()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment