/* James P. McEldoon * CS 221 */ import java.util.Random; 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 * Counts the occurences of each element in each array. * If the number of occurences is the same for each element in the array then returns true */ public static boolean isPermutation(int[] listA, int[] listB) { assert (listA != null && listB != null) : "Violation of precondition: isPermutation"; int cntA; //count of element n in array A int cntB; //count of element n in array B int number; //element n in array A at position i boolean permutation = true; if(listA.length!=listB.length){ //If arrays are not the same length return false permutation=false; } if(permutation){ for(int i=0; ilist.length/2){ return j-1; } if(i>list.length/2){ return -1; } }//for 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"; int newPosition; long time = System.currentTimeMillis(); Random generator = new Random(time); int [] a = new int[original.length]; System.arraycopy( original, 0, a, 0, original.length ); for(int i=0; i 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"; for(int i=0; i9){ //0 is valid for incomplet board result=1; } } if(result==3){ for(int i=0; i<9; i++){ if(problemArray[i]>0) doubleNumber[problemArray[i]]++; if(doubleNumber[problemArray[i]]>1){ result=2; } } //System.out.println("problemFound result = " + result); } return result; } /** * 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, col, 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"; int result; debugMess("Row Check"); result = rowCheck(board); if(result==0 || result==3){ debugMess("Col Check"); result = colCheck(board, result); } if(result==0 || result ==3){ debugMess("SubBox Check"); result = subBoxCheck(board); } return result; } /** * Used for debuging * @param Message string */ private static void debugMess(String m) { if(debug){ System.out.println(m); } } private static boolean debug; public static void main(String[] args) { if(args.length==0){ debug=false; } else{ debug=true; debugMess("Debug messages enabled with arg[0] != null"); } int result; //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"); printList(a); printList(b); //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"); printList(a); printList(b); //test 3 if( findMajority(b) == -1 ) System.out.println("passed test 3"); else System.out.println("failed test 3"); printList(b); //test 4 b[0] = 3; result = findMajority(b); if( result == 0 || result == 2 || result == 3 ) System.out.println("passed test 4"); else System.out.println("failed test 4 "+ result); printList(b); //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"); printList(a); printList(b); //test 7 b = new int[]{0}; if( nonDecreasing(b)) System.out.println("passed test 7"); else System.out.println("failed test 7"); printList(b); //test 8 sBoard[8][7] = 4; result = sudokuStatus(sBoard); if( result == 2) //change to 2, there is a duplicate in a column System.out.println("passed test 8"); else{ System.out.println("failed test 8"); debug = true; System.out.println("result = " + result); for(int i=0; i<9; i++){ printList(sBoard[i]); } debug = false; } //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 == 0) 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 nonDecreasing"); printList(b); //test 12 b = new int[] {0, 2, 4}; if( nonDecreasing(b)) System.out.println("passed test 12"); else System.out.println("failed test 12 nonDecreasing"); printList(b); //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 nonDecreasing"); printList(b); //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 nonDecreasing"); printList(b); //test 15 b = new int[] {0, 2, 1}; if( !nonDecreasing(b)) System.out.println("passed test 15"); else System.out.println("failed test 15 nonDecreasing"); printList(b); //test 16 sBoard = new int[][] { {7, 5, 1, 8, 6, 3, 9, 2, 4}, {8, 10, 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 == 1) System.out.println("passed test 16"); else System.out.println("failed test 16"); //test 17 sBoard = new int[][] { {7, 5, 1, 8, 6, 3, 9, 2, 4}, {8, 2, 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 == 2) System.out.println("passed test 17"); else System.out.println("failed test 17"); //System.out.println("result = " + result); //test 18 sBoard = new int[][] { {7, 5, 1, 8, 6, 3, 9, 2, 4}, {8, 4, 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 == 2) System.out.println("passed test 18"); else System.out.println("failed test 18"); //System.out.println("result = " + result); //test 19 sBoard = new int[][] { {7, 5, 1, 8, 6, 3, 9, 2, 4}, {8, 4, 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 == 1) System.out.println("passed test 19"); else System.out.println("failed test 19"); //System.out.println("result = " + result); //test 20 sBoard = new int[][]{ {7, 0, 1, 8, 0, 0, 9, 0, 0}, {0, 3, 0, 4, 4, 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 == 2) System.out.println("passed test 21"); else System.out.println("failed test 21"); //test 21 sBoard = new int[][]{ {7, 0, 1, 8, 0, 0, 9, 0, 0}, {0, 3, 0, 4, 0, 0, 6, 5, 0}, {9, 0, 0, 4, 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 == 2) System.out.println("passed test 21"); else System.out.println("failed test 21"); }// 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; } }