Skip to content

Commit

Permalink
new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
valde148 committed Sep 20, 2024
1 parent 5fde192 commit 0bad427
Showing 1 changed file with 78 additions and 3 deletions.
81 changes: 78 additions & 3 deletions main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

class main{
Expand All @@ -9,7 +12,10 @@ public static void main(String[] args){
int pro = s.nextInt();

int[][] programerPreferences = new int[pro][pro];
char[][] companyPreferences = new char[pro][pro];
int[][] companyPreferences = new int[pro][pro];
// int[][] programerPreferences = {{1, 2, 0}, {0, 1 , 2}, {2, 1, 0}};
// int[][] companyPreferences = {{1,2,0},{0,1,2},{2,1,0}};

char[] alphabet = {'a', 'b' , 'c', 'd', 'e', 'f', 'g'};

for(int i = 0; i < pro; i++){
Expand All @@ -21,14 +27,83 @@ public static void main(String[] args){
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.next().charAt(0);
companyPreferences [x][y] = s.nextInt();
//companyPreferences[x][y] = s.next().charAt(0);
}
}
int[] matches = match(programerPreferences, companyPreferences);

System.out.println("Preferences in Array form \nprogramers: companies:");
for (int i = 0; i < pro; i++) {
int companyId = i + 1;
System.out.println(alphabet[i] + Arrays.toString(programerPreferences[i]) + " " + companyId + Arrays.toString(companyPreferences[i]));
}


for (int i : matches) {
// if(i % 2 == 0) {
// System.out.println(alphabet[i]);
// } else System.out.println(i);
System.out.println(i);
}
}

static int[] match(int[][] programerPreferences, int[][] companyPreferences) {
Map<Integer, Integer> temp = new HashMap<>();
int size = programerPreferences.length;
boolean[] programersFree = new boolean[size];
boolean[] companiesFree = new boolean[size];
Arrays.fill(programersFree, true);
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;
break;
} else {
int possibleProgramer = temp.get(programerPreferences[i][j]);
boolean match = checkMatch(possibleProgramer, i , programerPreferences[i][j], companyPreferences);
if( !match){
temp.put(programerPreferences[i][j], i);
programersFree[i] = false;
programersFree[possibleProgramer] = true;
break;
}
}
}
if (programersFree[i]) {
stillFree.add(i);
}
}
}
}
int[] matchedPairs = new int[2*temp.size()];
int i = 0;
for(Map.Entry<Integer, Integer> entry : temp.entrySet()) {
matchedPairs[i] = entry.getValue();
i++;
matchedPairs[i] = entry.getKey();
i++;
}
return matchedPairs;
}

static boolean checkMatch(int possibleProgramer, int currentProgramer, int company, int[][] companyPreferences) {
int p = 0; //index of possible possibleProgramer
int c = 0; //index of currentProgramer
for (int i = 0; i < companyPreferences.length; i++) {
if(companyPreferences[company][i] == possibleProgramer) {
p = i;
} else if (companyPreferences[company][i] == possibleProgramer){
c = 1;
}
}
return p < c;
}
}
}

0 comments on commit 0bad427

Please sign in to comment.