FRQ 2018 Q4

public class ArrayTester {
    public static int[] getColumn(int[][] arr2D, int c) {
        int[] result = new int[arr2D.length];

        // iterate through rows
        for (int i = 0; i < arr2D.length; i++) {
            // assign element c in the row to result[i]
            result[i] = arr2D[i][c];
        }

        return result;
    }

    public static boolean hasAllValues(int[] arr1, int[] arr2) {
        // always true simply for testing
        return true;
    }

    public static boolean containsDuplicates(int[] arr) {
        return false;
    }

    public static boolean isLatin(int[][] square) {

        // check rows of square for duplicates
        for (int i = 0; i < square.length; i++) {
            if (containsDuplicates(square[i])) {
                return false;
            }
            // compare rows against row 0, return false if not the same
            if (!hasAllValues(square[0], square[i])) {
                return false;
            }
        }

        // check columns for duplicates
        for (int i = 0; i < square[0].length; i++) {
            // get column at index
            int[] col = getColumn(square, i);
            // check for duplicates in col
            if (containsDuplicates(col)) {
                return false;
            }
            // compare columns against column 0, return false if not the same
            if (!hasAllValues(getColumn(square, 0), col)) {
                return false;
            }
        }

        // if passes all checks return true
        return true;

    }

    public static void main(String[] args) {
        int[][] arr2D = {
            {0,1,2},
            {3,4,5},
            {6,7,8},
            {9,5,3}
        };

        int[] result = ArrayTester.getColumn(arr2D, 1);

        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i] + " ");
        }

        int[][] latinSq = {
            {1,2,3},
            {2,3,1},
            {3,1,2}
        };

        System.out.println("");
        System.out.println("Is latin: " + isLatin(latinSq));
    }
}

ArrayTester.main(null);
1 4 7 5 
Is latin: true

FRQ 2017 Q4

public class Position {
    int row;
    int col;

    public Position(int r, int c) {
        /* Implementation not shown */
        this.row = r;
        this.col = c;
    }

    public String toString() {
        String s = "(" + this.row + "," + this.col + ")";
        return s;
    }

    public static Position findPosition(int val, int[][] arr) {
        // double for loop to iterate through rows and columns inside each row
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (val == arr[i][j]) {
                    Position p = new Position(i,j);
                    return p;
                }
            }
        }

        return null;
    }
    
    public static Position[][] getSuccessorArray(int[][] intArr) {
        // initialize position array with appropriate size
        Position[][] successor = new Position[intArr.length][intArr[0].length];

        // double for loop to iterate through rows and columns
        for (int i = 0; i < intArr.length; i++) {
            for (int j = 0; j < intArr[i].length; j++) { 
                // set successor Position object to the location of the value of intArr[i][j] + 1
                successor[i][j] = findPosition(intArr[i][j] + 1, intArr);
            }
        }
        return successor;
    }

    public static void main(String[] args) {
        int[][] arr2D = {
            {1,2,3},
            {4,5,6},
            {7,8,9}
        };

        System.out.println(findPosition(6, arr2D));

        Position[][] result = getSuccessorArray(arr2D);
        // print result from successor
        for (int i = 0; i < result.length; i++) {
            System.out.println("");
            for (int j = 0; j < result[i].length; j++) { 
                System.out.print(result[i][j] + " ");
            }
        }
    }
}

Position.main(null);
(1,2)

(0,1) (0,2) (1,0) 
(1,1) (1,2) (2,0) 
(2,1) (2,2) null 

FRQ 2016 Q4

public class StringFormatter {
    public static int totalLetters(List<String> wordList) {
        int total = 0;

        // iterate through list, get length of each item
        for (int i = 0; i < wordList.size(); i++) {
            total += wordList.get(i).length();
        }

        return total;
    }

    public static int basicGapWidth(List<String> wordList, int formattedLen) {
        int wordCount = wordList.size();
        int totalLetters = totalLetters(wordList);

        // number of spaces available
        int spaceCount = formattedLen - totalLetters;

        // number of spaces divided by number of spaces between words
        return spaceCount / (wordCount - 1);
    }

    // implemented leftoverspaces so that can try out testcases
    public static int leftoverSpaces(List<String> wordList, int formattedLen) {
        int gapWidth = basicGapWidth(wordList, formattedLen);
        int gapCount = wordList.size() - 1;
        
        // number of spaces available
        int spaceCount = formattedLen - totalLetters(wordList);

        return (spaceCount - (gapWidth * gapCount));
    }

    public static String format(List<String> wordList, int formattedLen) {
        int gapWidth = basicGapWidth(wordList, formattedLen);
        int leftoverSpaces = leftoverSpaces(wordList, formattedLen);
        String formatted = "";

        // for loop for number of gaps
        for (int i = 0; i < (wordList.size() - 1); i++) {
            // append first word
            formatted += wordList.get(i);

            // append number of spaces per gap
            for (int j = 0; j < gapWidth; j++) {
                formatted += " ";
            }

            // append an extra space if available, decrement
            if (leftoverSpaces > 0) {
                formatted += " ";
                leftoverSpaces--;
            }

        }

        // append last word
        formatted += wordList.get(wordList.size() - 1);

        return formatted;
    }

    public static void main(String[] args) {
        List<String> apcsa = new ArrayList<String>();
        apcsa.add("AP");
        apcsa.add("COMP");
        apcsa.add("SCI");
        apcsa.add("ROCKS");

        System.out.println(format(apcsa, 20));

        List<String> geah = new ArrayList<String>();
        geah.add("GREEN");
        geah.add("EGGS");
        geah.add("AND");
        geah.add("HAM");

        System.out.println(format(geah, 20));

        List<String> bb = new ArrayList<String>();
        bb.add("BEACH");
        bb.add("BALL");

        System.out.println(format(bb, 20));
    }
}

StringFormatter.main(null);
AP  COMP  SCI  ROCKS
GREEN  EGGS  AND HAM
BEACH           BALL