/* Student information for assignment * */ import java.util.Random; /** * ArrayProblems.java * * VERY IMPORTANT: For this assignment only, the only methods and classes from the * Java standard library you may use in your final solutions are: * 1. the System.arraycopy method * 2. the Math class and any of its methods * 3. the Random class and any of its methods */ public class ArrayProblems { /** * determine if one array of ints is a permutation of another * @param listA != null * @param listB != null * @return true if listB is a permutation of listA, false otherwise */ public static boolean isPermutation(int[] listA, int[] listB) { assert (listA != null && listB != null) : "Violation of precondition: isPermutation"; return true; } /** * determine if there is a majority element in an array of ints * @param list != null * @return an index of the value of the majority element if it exists. * If a majority element does not exist return -1. */ public static int findMajority(int[] list) { assert ( list != null ) : "Violation of precondition: findMajority"; return -1; } /** * return a shuffled version of an array of ints * @param original != null * @return a shuffled version of original. Original is not altered by this * method. Each possible permutation of original has a roughly uniform chance * of being returned. */ public static int[] shuffle(int[] original) { assert original != null : "Violation of precondition: shuffle"; return new int[] {}; } /** * determine if the elements of an array are non decreasing starting at index 0 * pre: list != null, list.length > 0 * post: return true if the elements of an array are non decreasing, false otherwise * @param list the array to check * @return true if the elements of an array are non decreasing, false otherwise */ public static boolean nonDecreasing(int[] list) { assert (list != null) && (list.length > 0) : "Violation of precondition: maxSum"; return true; } /** * determine the status of a sudoku puzzle board. 0s represent open spaces. * pre: board != null, board.length = 9, board is a square matrix. * post: return 0 if the board is solved, * 1 if it is incorrent due to an invalid digit, * 2 if it is incorrect due to a duplicate digit in a row, column, or sub region * 3 if it is a valid in progress board * @param board the sudoku board * @return an int based on the status of the board */ public static int sudokuStatus(int[][] board){ assert (board != null) && board.length == 9 && isSquare(board) : "Violation of precondition: sudokuStatus"; return 0; } public static void main(String[] args) { //test 1 int[] a = {1, 2, 3}; int[] b = {2, 1, 3}; if ( isPermutation(a,b) ) System.out.println("passed test 1"); else System.out.println("failed test 1"); //test 2 b = new int[]{2, 1, 3, 3}; int[] c = {2, 1, 2, 3 }; if ( !isPermutation(a,b) ) System.out.println("passed test 2"); else System.out.println("failed test 2"); //test 3 if( findMajority(b) == -1 ) System.out.println("passed test 3"); else System.out.println("failed test 3"); //test 4 b[0] = 3; int result = findMajority(b); if( result == 0 || result == 2 || result == 3 ) System.out.println("passed test 4"); else System.out.println("failed test 4"); //test 5 int[][] sBoard = { {7, 0, 1, 8, 0, 0, 9, 0, 0}, {0, 3, 0, 4, 0, 0, 6, 5, 0}, {9, 0, 0, 0, 5, 0, 3, 0, 0}, {0, 2, 0, 1, 0, 0, 4, 0, 0}, {3, 0, 0, 0, 8, 0, 0, 0, 5}, {0, 0, 4, 0, 0, 5, 0, 7, 0}, {0, 0, 5, 0, 2, 0, 0, 0, 3}, {0, 1, 3, 0, 0, 6, 0, 4, 0}, {0, 0, 9, 0, 0, 8, 5, 0, 2}}; result = sudokuStatus(sBoard); if( result == 3) System.out.println("passed test 5"); else System.out.println("failed test 5"); //test 6 b = new int[] {2, 5, 5, 5, 3, 6, 7}; a = shuffle(b); if( isPermutation(a,b) ) System.out.println("passed test 6"); else System.out.println("failed test 6"); //test 7 b = new int[]{0}; if( nonDecreasing(b)) System.out.println("passed test 7"); else System.out.println("failed test 7"); //test 8 sBoard[8][7] = 4; result = sudokuStatus(sBoard); if( result == 3) System.out.println("passed test 8"); else System.out.println("failed test 8"); //test 9 sBoard[8][7] = 1; result = sudokuStatus(sBoard); if( result == 3) System.out.println("passed test 9"); else System.out.println("failed test 9"); //test 10 sBoard = new int[][] { {7, 5, 1, 8, 6, 3, 9, 2, 4}, {8, 3, 2, 4, 1, 9, 6, 5, 7}, {9, 4, 6, 7, 5, 2, 3, 8, 1}, {5, 2, 8, 1, 9, 7, 4, 3, 6}, {3, 6, 7, 2, 8, 4, 1, 9, 5}, {1, 9, 4, 6, 3, 5, 2, 7, 8}, {4, 8, 5, 9, 2, 1, 7, 6, 3}, {2, 1, 3, 5, 7, 6, 8, 4, 9}, {6, 7, 9, 3, 4, 8, 5, 1, 2}}; result = sudokuStatus(sBoard); if( result == 9) System.out.println("passed test 10"); else System.out.println("failed test 10"); //test 11 b = new int[] {0, 0, 0}; if( nonDecreasing(b)) System.out.println("passed test 11"); else System.out.println("failed test 11"); //test 12 b = new int[] {0, 2, 4}; if( nonDecreasing(b)) System.out.println("passed test 12"); else System.out.println("failed test 12"); //test 13 b = new int[] {0, 0, 1, 1, 2, 2, 3, 4, 1000}; if( nonDecreasing(b)) System.out.println("passed test 13"); else System.out.println("failed test 13"); //test 14 b = new int[] {0, 0, 1, 0, 1, 1,}; if( nonDecreasing(b)) System.out.println("passed test 14"); else System.out.println("failed test 14"); //test 17 b = new int[] {0, 2, 1}; if( !nonDecreasing(b)) System.out.println("passed test 15"); else System.out.println("failed test 15"); }// end main /* pre: mat != null post: return true if mat is a square matrix, false otherwise */ private static boolean isSquare(int[][] mat) { assert mat != null : "Violation of precondition: isSquare"; final int numRows = mat.length; int row = 0; boolean square = true; while( square && row < numRows ) { square = ( mat[row] != null) && (mat[row].length == numRows); row++; } return square; } /* pre: mat != null, valid != null post: return true if all elements in mat are one of the characters in valid */ private static boolean onlyContains(char[][] mat, char[] valid) { assert mat != null && valid != null : "Violation of precondition: onlyContains"; int row = 0; int col; boolean correct = true; while( correct && row < mat.length) { col = 0; while(correct && col < mat[row].length) { correct = contains(valid, mat[row][col]); col++; } row++; } return correct; } /* pre: list != null post: return true if c is in list */ private static boolean contains(char[] list, char c) { assert ( list != null ) : "Violation of precondition: contains"; boolean found = false; int index = 0; while( !found && index < list.length) { found = list[index] == c; index++; } return found; } /* pre: mat != null, mat.length > 0 * post: return true if mat is rectangular */ private static boolean isRectangular(int[][] mat) { assert (mat != null) && (mat.length > 0) : "Violation of precondition: isRectangular"; boolean correct = true; final int numCols = mat[0].length; int row = 0; while( correct && row < mat.length) { correct = (mat[row] != null) && (mat[row].length == numCols); row++; } return correct; } }