package main.java.exercise;

import main.java.framework.StudentInformation;
import main.java.framework.StudentSolution;

import java.util.LinkedList;

public class StudentSolutionImplementation implements StudentSolution {
    @Override
    public StudentInformation provideStudentInformation() {
        return new StudentInformation(
                "Joaquin", // Vorname
                "Telleria", // Nachname
                "01408189" // Matrikelnummer
        );
    }

    // Implementieren Sie hier Ihre Lösung für die Indexsuche
    public int findIndex(int[] numbers, int element) {

        for(int i = 0; i < numbers.length; i++){
            if (numbers[i] == element){
                return i;
            }
        }
        return -1;
    }

    // Implementieren Sie hier den Gale-Shapley-Algorithmus
    public void performGaleShapley(StableMatchingInstance instance, StableMatchingSolution solution) {


        int numberOfChildren = instance.getN();
        System.out.println("we start with " + numberOfChildren + " children");


        int[] next = new int[numberOfChildren]; // initialize the array that encodes for each child the next family to be checked for, for example if next[1] = 2, that means for child 1 next family to check is family 2
        for(int i = 0; i < numberOfChildren; i++){
            next[i] = 0;
        }

       while(solution.hasUnassignedChildren()){ // while (a child is free and can choose a family)

           int child = solution.getNextUnassignedChild(); // choose a child s
           int family = instance.getFamilyOfChildAtRank(child,next[child]);   // f is the first family in the preference list of s that has not chosen s yet

           if (solution.isFamilyFree(family)){ // if if s free
               solution.assign(child,family); // assign family to child in the solution
           }
           // elseif f prefers s to their current partner s'
                     // connect s and f
                    // disconnect s' and f
           // else gets rejected (remains free)

           next[child] = next[child] + 1;


       }



    }

    // Implementieren Sie hier Ihre Methode zur Überprüfung, ob ein Matching stabil ist.
    public boolean isStableMatching(StableMatchingInstance instance, StableMatchingSolution solution) {


        return true;
    }
}

@@ -118,37 +118,8 @@
 n;%0A%0A
-import java.util.LinkedList;%0A
 %0Apub
@@ -880,33 +880,32 @@
 on solution) %7B%0A%0A
-%0A
         int numb
@@ -1016,17 +1016,16 @@
 ren%22);%0A%0A
-%0A
         
@@ -1311,16 +1311,51 @@
 i%5D = 0;%0A
+            solution.setFree((i));%0A
         
@@ -1364,16 +1364,17 @@
 %0A       
+ 
 while(so
@@ -1458,16 +1458,17 @@
 amily)%0A%0A
+ 
         
@@ -1544,24 +1544,25 @@
 %0A           
+ 
 int family =
@@ -1613,18 +1613,16 @@
 child%5D);
-  
  // f is
@@ -1693,17 +1693,150 @@
 n s yet%0A
-%0A
+            int currentChildAssignedToFamily = solution.getAssignedChild(family); // s' is the current child assigned to this family%0A 
         
@@ -1890,16 +1890,17 @@
  s free%0A
+ 
         
@@ -1994,20 +1994,140 @@
     
-%7D%0A          
+ %7D else if (instance.getRankOfChildForFamily(family,child) %3C instance.getRankOfChildForFamily(family,currentChildAssignedToFamily))%7B
  // 
@@ -2189,24 +2189,70 @@
         
-    
+solution.setFree(currentChildAssignedToFamily);
  // 
+dis
 connect 
@@ -2252,16 +2252,17 @@
 onnect s
+'
  and f%0A 
@@ -2280,18 +2280,42 @@
     
-   
+solution.assign(child,family);
  // 
-dis
 conn
@@ -2311,33 +2311,32 @@
 y); // connect s
-'
  and f%0A         
@@ -2341,31 +2341,86 @@
     
-// else gets rejecte
+ %7D%0A            else%7B%0A                  solution.setFree(child); // else chil
 d 
-(
 rema
@@ -2427,19 +2427,32 @@
 ins free
-)%0A%0A
+%0A            %7D%0A 
         
@@ -2485,18 +2485,17 @@
 d%5D + 1;%0A
-%0A%0A
+ 
        %7D
@@ -2496,18 +2496,16 @@
      %7D%0A%0A
-%0A%0A
     %7D%0A%0A 

Update StudentSolutionImplementation.java