Skip to content

Instantly share code, notes, and snippets.

@simkimsia
Last active December 20, 2015 06:58
Show Gist options
  • Select an option

  • Save simkimsia/6089357 to your computer and use it in GitHub Desktop.

Select an option

Save simkimsia/6089357 to your computer and use it in GitHub Desktop.
puzzle solved for some silly programming problems in academic school
import java.util.*;
import java.lang.*;
public class PuzzleSolved {
public static void main (String[] args) {
int m = 4; // m is the number of sub puzzles OR number of digits, e.g. 4
int k = 2; // k is the puzzle difficulty OR bit space per digit, e.g. 2
int maxDigitSize = (int) Math.pow(2, k) - 1; // e.g. we expect this to be 3
int x = maxDigitSize + 1; // x represents the x-nary space we are in. binary means x is 2.
int maxCase = maxDigitSize * m; // e.g. max is 12 because it looks like 3333 in 4-nary
int cases = maxCase + 1; //because we start from 0 to max case.
int maxXNaryNumber = (int) Math.pow(x, m) - 1; // expressed in decimal. e.g. 3333 in 4-nary means 255
// since no number can store 0110, we use String
// since we are using String, so 6363 could be 63.63. or 636.3. we will use dots to separate
// hence 0110 would be 0.1.1.0.
String xNaryNumber = "";
// assuming 4-nary.
// we need to traverse from 0000 to 3333
// means we need to go from 0 to 255.
// however, we only have 13 cases, i.e. 0 to 12.
// but some of 4-nary numbers may share same case but mean different values
// e.g. 0030 and 0003 share the case 3 but 0030 means 12 and 0003 means 3.
// the algo is
// 1) we find out the x-nary space is how large in decimal values
// 2) make a list of x-nary numbers in that space
// 3) each x-nary number is a string. we can use this as reference to make the strings
// See http://www.wikihow.com/Convert-from-Decimal-to-Binary method 1
// 4) make a list of case and their frequencies
// 5) then we sum up the digits of each x-nary number which will surely match up to one of the cases
// 6) we increment the case frequency each time we discover a x-nary number that matches the case number
// 1) find how large x-nary??
int xNarySpaceSize = maxXNaryNumber + 1;
// 2) make a list of x-nary numbers in that space
// 3) each x-nary number is a string. we can use this as reference to make the strings
ArrayList <String> xNaryList = makeXNaryList(xNarySpaceSize, x);
// 4) make a list of case and their frequencies
ArrayList <Integer> caseFreqList = makeCaseFrequencyList(cases);
// 5) then we sum up the digits of each x-nary number
for(int i = 0; i<xNarySpaceSize; i++) {
xNaryNumber = xNaryList.get(i);
int caseNumber = sumUpDigits(xNaryNumber);
// 6) we increment the case frequency
caseFreqList = incrementFrequency(caseFreqList, caseNumber);
}
// 7) print
printCaseFrequency(caseFreqList);
}
/**
*
* implementation of http://www.wikihow.com/Convert-from-Decimal-to-Binary method 1
*/
public static String convertDecimalToXnary(int decimal, int x) {
int quotient = -1;
int remainder = 0;
String result = "";
do {
quotient = decimal / x;
remainder = decimal % x;
result = remainder + "." + result;
// need to replace the decimal with the quotient before
// looping
decimal = quotient;
} while(quotient != 0);
return result;
}
/**
* sum up all the digits in a x-nary number
*/
public static int sumUpDigits(String xNaryNumber) {
// @HINT feel free to see all the xnary numbers here
// System.out.println(xNaryNumber);
// read http://stackoverflow.com/a/3481842/80353
// on how to do string split
String[] digits = xNaryNumber.split("\\.");
int sum = 0;
for(int i=0;i<digits.length; i++) {
sum += Integer.parseInt(digits[i]);
}
return sum;
}
/**
* index represents case number. value represents frequency
*/
public static ArrayList <Integer> makeCaseFrequencyList(int size) {
ArrayList <Integer> caseFreqList = new ArrayList <Integer> (size);
// make all cases start at 0 frequency
for(int i=0; i<size; i++) {
caseFreqList.add(i, 0);
}
return caseFreqList;
}
/**
* size is how big the space
* x is the x-nary space
*/
public static ArrayList <String> makeXNaryList(int size, int x) {
ArrayList <String> xNaryList = new ArrayList <String> (size);
for(int i=0; i<size; i++) {
String xNary = convertDecimalToXnary(i, x);
xNaryList.add(i, xNary);
}
return xNaryList;
}
/**
* given case number, we want to increase the frequency of case number by 1
*/
public static ArrayList <Integer> incrementFrequency(ArrayList <Integer> caseFreqList, int caseNumber) {
int currentFreq = caseFreqList.get(caseNumber);
currentFreq++;
caseFreqList.set(caseNumber, currentFreq);
return caseFreqList;
}
/**
* print out the case frequency
*/
public static void printCaseFrequency(ArrayList <Integer> caseFreqList) {
int size = caseFreqList.size();
for(int i=0; i<size; i++) {
System.out.println("case " + i + ": " + caseFreqList.get(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment