Created
October 5, 2022 06:59
-
-
Save ProfAndreaPollini/79e794740c1889bf3a278cab0004ab99 to your computer and use it in GitHub Desktop.
Estrattore Doppio - Estrarre due permutazioni di elementi in modo tale che non vi siano elementi uguali nelle stesse posizioni
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package estrazioni; | |
import java.util.Vector; | |
import java.util.concurrent.ThreadLocalRandom; | |
public abstract class Estrattore { | |
protected Vector<Studente> studenti; | |
protected Estrattore() { | |
studenti = new Vector<>(); | |
} | |
protected static int getRandomIdx(Vector<Studente> estratti) { | |
return ThreadLocalRandom.current().nextInt(estratti.size()); | |
} | |
protected static void scambiaPosizioni(Vector<Studente> estratti, int idx1, int idx2) { | |
var tmp = estratti.get(idx1); | |
estratti.set(idx1, estratti.get(idx2)); | |
estratti.set(idx2,tmp); | |
} | |
protected static Vector<Studente> copiaVettoreStudenti(Vector<Studente> studenti) { | |
Vector<Studente> estratti = new Vector<>(); | |
for(var s: studenti) { | |
estratti.add(s); | |
} | |
return estratti; | |
} | |
public void add(Studente s) { | |
studenti.add(s); | |
} | |
public abstract Vector<Studente>estrai(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package estrazioni; | |
import java.util.Vector; | |
public class EstrattoreDoppio extends Estrattore{ | |
private final Vector<Studente> altraEstrazione; | |
public EstrattoreDoppio( | |
Vector<Studente> altraEstrazione) { | |
this.altraEstrazione = altraEstrazione; | |
this.studenti = copiaVettoreStudenti(altraEstrazione); | |
} | |
@Override | |
public Vector<Studente> estrai() { | |
Vector<Studente> estratti = mischiaStudenti(); | |
int i = 0; | |
while (i < estratti.size()) { | |
if(estratti.get(i).equals(altraEstrazione.get(i))) { | |
var idx = getRandomIdx(estratti); | |
scambiaPosizioni(estratti,i,idx); | |
i=0; | |
} else { | |
i++; | |
} | |
} | |
return estratti; | |
} | |
private Vector<Studente> mischiaStudenti() { | |
var estratti =copiaVettoreStudenti(studenti); | |
var e =new EstrattoreSemplice(20); | |
for(var estratto: estratti) { | |
e.add(estratto); | |
} | |
estratti = e.estrai(); | |
return estratti; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package estrazioni; | |
import java.util.Vector; | |
public class EstrattoreSemplice extends Estrattore{ | |
private final int numeroScambi; | |
public EstrattoreSemplice(int numeroScambi) { | |
super(); | |
this.numeroScambi = numeroScambi; | |
} | |
@Override | |
public Vector<Studente> estrai() { | |
Vector<Studente> estratti = copiaVettoreStudenti(studenti); | |
for (int i = 0; i < numeroScambi; i++) { | |
var idx1 = getRandomIdx(estratti); | |
var idx2 = getRandomIdx(estratti); | |
//System.out.println(String.format("%d %d",idx1,idx2)); | |
scambiaPosizioni(estratti, idx1, idx2); | |
// System.out.println(estratti); | |
} | |
return estratti; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package estrazioni; | |
public class Studente { | |
private final String nome; | |
public Studente(String nome) { | |
this.nome = nome; | |
} | |
public String getNome() { | |
return nome; | |
} | |
@Override | |
public String toString() { | |
return "Studente{" + | |
"nome='" + nome + '\'' + | |
'}'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package estrazioni; | |
public class Test { | |
public static void main(String[] args) { | |
var estrattore = new EstrattoreSemplice(5); | |
estrattore.add(new Studente("Pinco Pallo 1")); | |
estrattore.add(new Studente("Pinco Pallo 2")); | |
estrattore.add(new Studente("Pinco Pallo 3")); | |
estrattore.add(new Studente("Pinco Pallo 4")); | |
estrattore.add(new Studente("Pinco Pallo 5")); | |
var estrazione1 = estrattore.estrai(); | |
System.out.println(estrazione1); | |
var doppio = new EstrattoreDoppio(estrazione1); | |
var estrazione2 = doppio.estrai(); | |
System.out.println(estrazione2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment