Skip to content

Commit

Permalink
Fixes Display and fixes logic error in checkMatch function
Browse files Browse the repository at this point in the history
also adds doccumentation
  • Loading branch information
Bonesbhbhbh committed Sep 24, 2024
1 parent 3e5f0f5 commit 5280b8d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 47 deletions.
2 changes: 0 additions & 2 deletions README.me
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Todo:
- El finish formating for output
- El - Change for loops
- Armando - values to test on
- Armando- Questions
136 changes: 91 additions & 45 deletions main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pro; y++){
// companyPreferences [x][y] = s.nextInt();
// //companyPreferences[x][y] = s.next().charAt(0);
// }
// }
// int[][] companyPreferencesTranslated = translator(companyPreferences);
s.close();
// processes first input for programer preferences
String inputString1 = s.nextLine().toLowerCase();
char[] input1 = inputString1.toCharArray();
for (int j = 0; j < input1.length; j++) {
programerPreferencesScanner[1][j] = input1[j];
}
//processes rest of inputs
for (int i = 0; i < pro; i++) {
System.out.println("insert programer " + (i+1) + "'s preferences \nplease enter on one line with no spaces or commas");
String inputString = s.nextLine().toLowerCase();
char[] input = inputString.toCharArray();
for (int j = 0; j < input.length; j++) {
programerPreferencesScanner[i][j] = input[j];
}
}

int[] matches = match(companyPreferences, programerPreferencesTranslated);
//processes inputs for company preferences
for (int i = 0; i < pro; i++) {
System.out.println("insert preferences for company " + alphabet[i] + "\nplease enter each programer id number on a new line");
for (int j = 0; j < pro; j++) {
companyPreferencesScanner[i][j] = s.nextInt();
companyPreferencesTranslatedScanner[i][j] = companyPreferencesScanner[i][j] -1;
}
}
//ends scanner
s.close();
//translates user imputed programer preferences to something our method understands
int[][] programerPreferencesTranslated = translator(programerPreferencesScanner);
//calls match method
int[] matches = match(companyPreferencesTranslatedScanner, programerPreferencesTranslated);

//print statements
System.out.println("Preferences in Array form \nprogramers: companies:");
for (int i = 0; i < pro; i++) {
int programerId = i + 1;
// char c = (char) i;
System.out.println(programerId + Arrays.toString(programerPreferences[i]) + " " + (char)i + Arrays.toString(companyPreferences[i]));
// String c = Integer.toString(i + 1);
System.out.println(programerId + Arrays.toString(programerPreferencesScanner[i]) + " " + alphabet[i] + Arrays.toString(companyPreferencesScanner[i]));
}


for (int i = 0; i < matches.length; i++) {
if(i % 2 == 0) {
System.out.println("Match #"+ i/2);
System.out.println(matches[i]);
System.out.println("Match #"+ (i/2 + 1));
System.out.println(matches[i] +1);
// // System.out.println(i +1);
} else {
// char company = (char) matches[i];
System.out.println((char)matches[i]);
System.out.println(alphabet[matches[i]]);
}
}
}

public static int[][] translator(char[][] companyPreferences){
//takes character array and converts it to integers where a = 0, b = 1, c = 2, etc...
// logic to make everything lowercase is handled in scanner
int[][] companyPreferencesTranslated = new int[companyPreferences.length][companyPreferences[0].length];
for (int i = 0; i < companyPreferences.length; i++) {
for (int j = 0; j < companyPreferences[0].length; j++) {
char c = (char) companyPreferences[i][j];
companyPreferencesTranslated[i][j] = c;
char c = companyPreferences[i][j];
int n = c - 97;
companyPreferencesTranslated[i][j] = n;
}
}
return companyPreferencesTranslated;
}

static int[] match(int[][] programerPreferences, int[][] companyPreferences) {
// array construction
// temp tracks current pairs
// programersFree and companiesFree track who is not paired
// stillFree tracks who is still free
Map<Integer, Integer> temp = new HashMap<>();
int size = programerPreferences.length;
boolean[] programersFree = new boolean[size];
Expand All @@ -75,19 +117,20 @@ static int[] match(int[][] programerPreferences, int[][] companyPreferences) {
Arrays.fill(companiesFree, true);
ArrayList<Integer> 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;
Expand All @@ -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<Integer, Integer> entry : temp.entrySet()) {
Expand All @@ -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
}
}

0 comments on commit 5280b8d

Please sign in to comment.