Created
May 31, 2018 20:53
-
-
Save Jakemangan/c201d22a518c14b085b9659f87cb70e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Arrays; | |
class Challenge { | |
private static Integer[] input; | |
private static int inputLength; | |
private static String concatBinary; | |
private static int resultantValue; | |
public static Integer arrayPacking(List<Integer> integers) | |
{ | |
//Initialise values | |
input = null; | |
inputLength = 0; | |
concatBinary = ""; | |
resultantValue = 0; | |
//First ensure numbers are between 0 and 255, if not prevent further execution | |
if(checkNumberBoundaries(integers)) | |
{ | |
input = new Integer[integers.size()]; | |
input = integers.toArray(input); | |
inputLength = input.length; | |
concatBinary = createConcatenatedBinaryString(); | |
resultantValue = binaryToDecimal(concatBinary); | |
System.out.println("Result: " + resultantValue); | |
return resultantValue; | |
} | |
else | |
{ | |
System.out.println("Input numbers are not between 0 and 255"); | |
return -1; //Indicates error | |
} | |
} | |
/* | |
* Convert decimal value into binary representation | |
* @param int input - integer with decimal value to convert | |
* @return String outString - binary representation of integer parameter | |
*/ | |
public static String decimalToBinary(int input) | |
{ | |
String outString = Integer.toBinaryString(input); | |
//Concatenates a 0 onto the beginning of the string whilst the binary string's length is less than 8 | |
while(outString.length() < 8) | |
{ | |
outString = "0" + outString; | |
} | |
System.out.println("Output: " + outString); | |
return outString; | |
} | |
/* | |
* Create long binary string from concatenated binary strings | |
* @param - none | |
* @return - Long concatenated binary string | |
*/ | |
public static String createConcatenatedBinaryString() | |
{ | |
//For loop used in reverse to ensure correct placement of binary strings | |
for(int i = inputLength-1; i >= 0; i--) | |
{ | |
String binaryStr = decimalToBinary(input[i]); | |
concatBinary = concatBinary + binaryStr; | |
} | |
System.out.println("Long binary string: " + concatBinary); | |
return concatBinary; | |
} | |
/* | |
* Convert binary string back to decimal value | |
* @Param String input - String holding value of binary string to be converted to decimal | |
* @Return int decVal - integer representation of binary string parameter | |
*/ | |
public static int binaryToDecimal(String input) | |
{ | |
int decVal = Integer.parseInt(input, 2); | |
return decVal; | |
} | |
/* | |
* Ensures numbers held in input List are between 0-255 | |
* @Param List<Integer> input - List holding input integers | |
* @Return boolean inputOkay - boolean determining if List contents are legal | |
*/ | |
public static boolean checkNumberBoundaries(List<Integer> input) | |
{ | |
boolean inputOkay = true; | |
for(Integer n : input) | |
{ | |
if(n > 255 || n < 0) | |
{ | |
inputOkay = false; | |
} | |
} | |
return inputOkay; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment