diff --git a/README.me b/README.me index aa8a698..384ea15 100644 --- a/README.me +++ b/README.me @@ -1,5 +1,3 @@ Todo: -- El finish formating for output -- El - Change for loops - Armando - values to test on - Armando- Questions \ No newline at end of file diff --git a/main.java b/main.java index b98c0fa..f4d8893 100644 --- a/main.java +++ b/main.java @@ -8,65 +8,107 @@ class main{ public static void main(String[] args){ + char[] alphabet = {'a','b','c','d','e','f','g','h','i','j'}; + + //test 3x3 + //user entries: + //int[][] companyPreferences1 = {{2, 3, 1}, {1, 2, 3}, {3, 2, 1}}; //this is what is entered on the user side + //char[][] programerPreferences1 = {{'a','b','c'}, {'c','b','a'}, {'b','c','a'}}; //this is what is entered on the user side + //what the program receives: + //index equivalents of intended matches + int[][] companyPreferencesTranslated1 = {{1, 2, 0}, {0, 1 , 2}, {2, 1, 0}}; + int[][] programerPreferencesTranslated1 = {{0,1,2},{2,1,0},{1,2,0}}; + int[] expected = {1, 0, 0, 1, 2, 2}; + int[] returned = match(companyPreferencesTranslated1, programerPreferencesTranslated1); + System.out.println("test one status:" + (Arrays.equals(expected, returned))); + + //test 2x2 + //int[][] companyPreferences2 = {{1,2},{1,2}}; + //char[][] programerPreferences2 = {{'a','b'},{'b','a'}}; + int[][] companyPreferencesTranslated2 = {{0,1},{0,1}}; + int[][] programerPreferencesTranslated2 = {{0,1},{1,0}}; + int[] expected2 = {0, 0, 1, 1}; + int[] returned2 = match(companyPreferencesTranslated2, programerPreferencesTranslated2); + System.out.println("test two status:" + (Arrays.equals(expected2, returned2))); + Scanner s = new Scanner(System.in); - System.out.println("Enter about of Programers: "); + System.out.println("Enter amount of Programers: "); int pro = s.nextInt(); - // int[][] programerPreferences = new int[pro][pro]; - // int[][] companyPreferences = new int[pro][pro]; - int[][] companyPreferences = {{1, 2, 0}, {0, 1 , 2}, {2, 1, 0}}; - char[][] programerPreferences = {{'a','b','c'}, {'c','b','a'}, {'b','c','a'}}; - int[][] programerPreferencesTranslated = {{1,2,0},{0,1,2},{2,1,0}}; + //array constructors + char[][] programerPreferencesScanner = new char[pro][pro]; + int[][] companyPreferencesScanner = new int[pro][pro]; + int[][] companyPreferencesTranslatedScanner = new int[pro][pro]; - // for(int i = 0; i < pro; i++){ - // System.out.println("Insert preferences for programmer " + i ); - // for(int j = 0; j < pro; j++){ - // programerPreferences [i][j] = s.nextInt(); - // } - // } - // for(int x = 0; x < pro; x++){ - // System.out.println("Insert preferences for company " + x); - // for(int y = 0; y temp = new HashMap<>(); int size = programerPreferences.length; boolean[] programersFree = new boolean[size]; @@ -75,19 +117,20 @@ static int[] match(int[][] programerPreferences, int[][] companyPreferences) { Arrays.fill(companiesFree, true); ArrayList stillFree = new ArrayList<>(); - while(stillFree.size() + temp.size() < size){ - for (int i = 0, j = 0; i < companiesFree.length; i++) { - if (programersFree[i]) { - for(; j < size; j++){ - if (companiesFree[programerPreferences[i][j]]) { - temp.put(programerPreferences[i][j], i); - programersFree[i] = false; - companiesFree[programerPreferences[i][j]] = false; + while(stillFree.size() + temp.size() < size){ //while there is still unpaired people + for (int i = 0; i < companiesFree.length; i++) { // go through free companies + if (programersFree[i]) { //if the a programer is free + for(int j = 0; j < size; j++){ //for each of the companies they are interested in + if (companiesFree[programerPreferences[i][j]]) { //if the company is free + temp.put(programerPreferences[i][j], i); // log pair + programersFree[i] = false; //mark taken + companiesFree[programerPreferences[i][j]] = false; // mark taken break; } else { - int possibleProgramer = temp.get(programerPreferences[i][j]); - boolean match = checkMatch(possibleProgramer, i , programerPreferences[i][j], companyPreferences); - if( !match){ + int possibleProgramer = programerPreferences[i][j]; //else record index of programer + boolean match = checkMatch(possibleProgramer, i , programerPreferences[i][j], companyPreferences); //check if possibleProgram has higher interest from company than current hire + if(!match){ + // replace current match with new programer temp.put(programerPreferences[i][j], i); programersFree[i] = false; programersFree[possibleProgramer] = true; @@ -96,11 +139,13 @@ static int[] match(int[][] programerPreferences, int[][] companyPreferences) { } } if (programersFree[i]) { + //log that programer needs to go through the process again stillFree.add(i); } } } } + // puts matched pairs from the hashMap to an array to be returned int[] matchedPairs = new int[2*temp.size()]; int i = 0; for(Map.Entry entry : temp.entrySet()) { @@ -116,12 +161,13 @@ static boolean checkMatch(int possibleProgramer, int currentProgramer, int compa int p = 0; //index of possible possibleProgramer int c = 0; //index of currentProgramer for (int i = 0; i < companyPreferences.length; i++) { + //for each of the programers the company is interested in, record if the possibleProgramer or current hire is higher if(companyPreferences[company][i] == possibleProgramer) { p = i; - } else if (companyPreferences[company][i] == possibleProgramer){ - c = 1; + } else if (companyPreferences[company][i] == currentProgramer){ + c = i; } } - return p < c; + return p < c; // respond if the possible programer is of higher interest } }