Mi sono rotto il cazzo

This commit is contained in:
eslusarz
2026-06-20 19:23:26 +02:00
parent ddf329482a
commit 2a51a8993d
19 changed files with 571 additions and 1 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+86 -1
View File
@@ -615,6 +615,91 @@ public class LinkedBinaryTree<E> implements BinaryTree<E>{
return Integer.compare(currentData, leftData + rightData) == 0; return Integer.compare(currentData, leftData + rightData) == 0;
} }
public boolean esisteNodoConFigliUguali() {
if (root == null) return false;
return esisteNodoConFigliUguali(root);
}
public boolean esisteNodoConFigliUguali(BinaryNode<E> node) {
// Se è una foglia
if (node.getLeft() == null && node.getRight() == null) return false;
// Reperiamo il valore di sx
boolean leftValue = (node.getLeft() != null) ? esisteNodoConFigliUguali(node.getLeft()) : false;
// Risali lo stack velocemente
if (leftValue) return true;
// Reperiamo il valore di dx
boolean rightValue = (node.getRight() != null) ? esisteNodoConFigliUguali(node.getRight()) : false;
// Risali lo stack velocemente
if (rightValue) return true;
// Controllo sui valori attuali
if (node.getLeft() != null && node.getLeft().getData() != null && node.getRight() != null && node.getRight().getData() != null) return node.getLeft().getData().equals(node.getRight().getData());
return false;
}
/*
* Realizzare un metodo generico statico
* public static <E> boolean TwinChildren(BinaryNode<E> root)
* che, dato in input l'albero radicato in root, restituisce TRUE se esiste almeno un nodo
* che ha esattamente due figli e questi figli sono uguali, FALSE altrimenti.
*/
public static <E> boolean TwinChildren(BinaryNode<E> root) {
// Null check (solo su ROOT)
if (root == null) throw new NullPointerException();
// Controllo se è una foglia
if (root.getLeft() == null && root.getRight() == null) return false;
// A questo punto non è una foglia, scendiamo e controlliamo...
boolean leftValue = (root.getLeft() == null) ? false : TwinChildren(root.getLeft());
// Se è presente un valore vero, puoi ignorare il resto e risalire subito lo stack
if (leftValue) return true;
// Controlliamo il nodo destro
boolean rightValue = (root.getRight() == null) ? false : TwinChildren(root.getRight());
if (rightValue) return true;
// Controllo sui due figli
if (root.getLeft() == null || root.getLeft().getData() == null) return false;
if (root.getRight() == null || root.getRight().getData() == null) return false;
return root.getLeft().getData().equals(root.getRight().getData());
}
/*
Esercizio 2.
Un albero binario si dice bilanciato per peso se, per ogni nodo, la differenza
tra il numero di nodi del sottoalbero sinistro e quella del sottoalbero destro
è al massimo 1 in valore assoluto. Data la radice di un albero binario,
scrivere un metodo ricorsivo public static boolean isWeightBalanced(BinaryNode<?> root)
che verifichi se l'albero è bilanciato per peso.
*/
public static boolean isWeightBalanced(BinaryNode<?> root) {
// Null check (SOLO SUL ROOT)
if (root == null) throw new NullPointerException();
// Avvio la ricorsione
return getWeightBalanced(root) != -1;
}
public static int getWeightBalanced(BinaryNode<?> node){
// Controllo se è un nodo foglia
if (node.getLeft() == null && node.getRight() == null) return 1;
// Raccolgo la parte sx
int leftValue = (node.getLeft() == null) ? 0 : getWeightBalanced(node.getLeft());
if (leftValue == -1) return -1;
// Raccolgo la parte dx
int rightValue = (node.getRight() == null) ? 0 : getWeightBalanced(node.getRight());
if (rightValue == -1) return -1;
// Controllo
int diff = leftValue - rightValue;
if (diff < -1 || diff > 1) return -1;
return leftValue + rightValue + 1;
}
} }
+53
View File
@@ -2,6 +2,7 @@ package network.network;
import network.Graph; import network.Graph;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
@@ -53,6 +54,58 @@ public class Network<Vertex extends Comparable<? super Vertex>> implements Graph
} }
} }
} }
/*
TOTALE 2024-06-18
Implementare un costruttore della classe Network
che prende in input una lista di oggetti di oggetti di tipo Vertex
e costruisce un nuovo grafo orientato come segue:
- gli oggetti presenti in lista rappresentano i vertici
- per ogni coppia di vertici distinti x e y, generare con il meotodo
Math.random() un valore double d € [0, 1) e aggiungere un arco orientato
pesato <x,y,d> solo se d!=0
*/
public Network(Vertex[] V, Integer v1) {
// Null check
if (V == null) throw new NullPointerException();
// Oggetti in lista diventano i vertici
for (Vertex v : V) addVertex(v);
// Per ogni coppia di vertici distinti
for (int i = 0; i < V.length; i++) {
Vertex iVertex = V[i];
for (int j = 0; j < V.length; j++) {
if (i == j) continue;
Vertex jVertex = V[j];
Double weight = Math.random();
if (Double.compare(weight, 0.0) != 0) addEdge(iVertex, jVertex, weight);
}
}
}
/*
* Implementare un metodo costruttore della classe Network che prende in input due liste
* L1 e L2 di tipo ArrayList<Vertex> e costruisce un nuovo grafo orientato come segue:
* gli oggetti presenti nelle due liste rappresentano i vertici; per ogni coppia di vertici
* distinti x ∈ L1 e y ∈ L2, il grafo contiene larco orientato x→y, avente come peso un
* valore double -p se x<y oppure p se x>y, dove p ε[0,1) è generato con il metodo
* Math.random().
*/
public Network(ArrayList<Vertex> L1, ArrayList<Vertex> L2) {
// Null check
if (L1 == null || L2 == null) throw new NullPointerException();
// Aggiunta di tutti i vertici
for (Vertex ss : L1) addVertex(ss);
for (Vertex ss : L2) addVertex(ss);
// Per ogni coppia di vertici disitinti (crazy AF)
for (Vertex s1 : L1) {
for (Vertex s2 : L2) {
int cmp = s1.compareTo(s2);
Double p = Math.random();
if (cmp < 0) addEdge(s1, s2, -p);
if (cmp > 0) addEdge(s1, s2, p);
}
}
}
@Override @Override
public boolean equals(Object object) { public boolean equals(Object object) {
+140
View File
@@ -0,0 +1,140 @@
package totale.p160726;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Set;
import java.util.TreeSet;
import java.util.Iterator;
import java.time.LocalDate;
public class Corriere {
// Classi d'instanza
private TreeMap<Spedizione, LocalDate> currentSpedizioni = new TreeMap<Spedizione, LocalDate>();
private TreeSet<Spedizione> fixedSpedizioni = new TreeSet<Spedizione>();
// Metodi
// METODO A
/*
boolean aggiungiSpedizione(String codice, double peso, LocalDate data)
Inserisce una nuova spedizione nella mappa delle spedizioni in gestione, dati codice di tracking, peso e data di presa in carico. L'inserimento deve avvenire solo se la
spedizione non è
già presente
né tra quelle in gestione né tra quelle concluse.
Restituisce true in caso di successo, false altrimenti.
*/
public boolean aggiungiSpedizione(
String codice,
Double peso,
LocalDate data
) {
// Null check
if (codice == null || peso == null || data == null) throw new NullPointerException();
// Creazione della spedizione
Spedizione s = new Spedizione(codice, peso);
// Controllo se è presente una spedizione uguale tra quelle presenti
if (currentSpedizioni.containsKey(s)) return false;
// Controllo se è presnete una spedizione uguale tra quelle portate a termine
if (fixedSpedizioni.contains(s)) return false;
// Aggiunta della spedizione
currentSpedizioni.put(s, data);
return true;
}
// MEOTDO B
/*
boolean consegnaSpedizione(String codice)
Rimuove dalla mappa delle spedizioni in gestione la spedizione con
il codice indicato, solo se presente. Se la rimozione avviene con
successo, la spedizione deve essere inserita nell'insieme delle
spedizioni concluse. Restituisce true in caso di successo,
false altrimenti.
*/
public boolean consegnaSpedizione(String codice) {
// Null check
if (codice == null) throw new NullPointerException();
// Creo l'oggetto fittizio
Spedizione s = new Spedizione(codice, null);
// Verifico la presenza della spedizione nell'elenco delle spedizioni attuali.
/*
SPIEGAZIONE:
Il controllo funziona perfettamente anche non andando ad inserire
il dato relativo al peso, dal momento che containsKey sfrutta il
compareTo che è realizzato solo sulla base del codice e non del peso.
*/
if (!currentSpedizioni.containsKey(s)) return false;
// Rimuoviamo l'oggetto
Iterator<Spedizione> it = currentSpedizioni.keySet().iterator();
while(it.hasNext()) {
Spedizione sTmp = it.next();
if (!sTmp.equals(s)) continue;
it.remove();
fixedSpedizioni.add(sTmp);
return true;
}
// Aggiungo s alle spedizioni consegnate
return false;
}
// METODO C
/*
double pesoTotale()
Calcola e restituisce il peso complessivo di tutte le spedizioni ancora in gestione.
*/
public Double pesoTotale() {
Double tot = 0.0;
for (Spedizione s : currentSpedizioni.keySet()) {
tot += s.pesoKg();
}
return tot;
}
// METODO D
/*
List <Spedizione> spedizioniPerPesoECodice()
Restituisce una lista contenente tutte le spedizioni ancora in gestione,
ordinate per peso crescente e, a parità di peso, per codice di tracking crescente.
*/
public List<Spedizione> spedizioniPerPesoECodice() {
List<Spedizione> tmp = new ArrayList<Spedizione>(currentSpedizioni.keySet());
Comparator<Spedizione> cmp = new Comparator<Spedizione>() {
@Override
public int compare(Spedizione s1, Spedizione s2) {
int cmp = Double.compare(s1.pesoKg(), s2.pesoKg());
if (cmp != 0) return cmp;
return s1.codiceTraking().compareTo(s2.codiceTraking());
}
};
tmp.sort(cmp);
return tmp;
}
// METODO F
/*
Map<LocalDate, Integer> numeroSpedizioniInGestionePerData()
Restituisce una mappa che associa a ogni data di presa in carico il numero
di spedizioni ancora in gestione e non ancora concluse,
conteggiate tra quelle presenti nella struttura delle spedizioni in gestione.
*/
public Map<LocalDate, Integer> numeroSpedizioniInGestionePerData() {
// Creazione della struttura dati
Map<LocalDate, Integer> tmp = new HashMap<LocalDate, Integer>();
// Aggiunta delle occorrenze
for (Spedizione s : currentSpedizioni.keySet()) {
LocalDate d = currentSpedizioni.get(s);
if (tmp.get(d) == null) tmp.put(d, 1);
else tmp.put(d, tmp.get(d) + 1);
}
return tmp;
}
}
+53
View File
@@ -0,0 +1,53 @@
package totale.p160726;
public class Spedizione implements Comparable<Spedizione>{
// Variabili di istanza
private String codiceTracking;
private Double pesoKg;
// Metodo costruttore
public Spedizione(
String codiceTracking,
Double pesoKg
) {
this.codiceTracking = codiceTracking;
this.pesoKg = pesoKg;
}
// Metodi
public String codiceTraking() {
return codiceTracking;
}
public Double pesoKg() {
return pesoKg;
}
public void setCodiceTraking(String codiceTraking) {
this.codiceTracking = codiceTraking;
}
public void setPesoKg(Double pesoKg) {
this.pesoKg = pesoKg;
}
// Meotdi equals e compare to
@Override
public boolean equals(Object object) {
if (object == null) return false;
if (object == this) return true;
if (!(object instanceof Spedizione)) return false;
Spedizione oSpedizione = (Spedizione) object;
return this.codiceTracking.equals(oSpedizione.codiceTracking);
}
@Override
public int compareTo(Spedizione spedizione) {
return this.codiceTracking.compareTo(spedizione.codiceTracking);
}
}
+106
View File
@@ -0,0 +1,106 @@
package totale.p240618;
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Comparator;
import java.util.HashMap;
public class CorpoDocente {
/*
Mantiene in memoria tutta la lista docenti
*/
private TreeSet<Docente> corpoDocente = new TreeSet<Docente>(new Comparator<Docente>() {
@Override
public int compare(Docente d1, Docente d2) {
int cmp = Integer.compare(d1.anniRuolo(), d2.anniRuolo());
if (cmp != 0) return cmp;
return d1.CF().compareTo(d2.CF());
}
});
/*
Mantiene in memoria tutti i CF aggiunti in modo da rispettare unicità.
*/
private HashSet<String> cfDocenti = new HashSet<String>();
// Metodo 1
public boolean aggiungi(
String CF,
int anniRuolo,
String meccanografico
) {
// Input Check
if (CF == null || meccanografico == null) throw new NullPointerException();
if (anniRuolo < 0) throw new IllegalArgumentException();
// Se non presente, si aggiunge il CF
if (cfDocenti.contains(CF)) return false;
// Aggiunta dell'oggetto
cfDocenti.add(CF);
return corpoDocente.add(new Docente(CF, anniRuolo, meccanografico));
}
// Metodo 2
public boolean presente(String CF) {
// Input check
if (CF == null) throw new NullPointerException();
// Controllo la presenza
return cfDocenti.contains(CF);
}
// Metodo 3
public boolean cancella(String CF) {
// Input check
if (CF == null) throw new NullPointerException();
// Controllo la presenza
if (!cfDocenti.contains(CF)) return false;
// Se presente lo rimuovo
Iterator<Docente> iterator = corpoDocente.iterator();
while (iterator.hasNext()) {
Docente currentDocente = iterator.next();
if (!currentDocente.CF().equals(CF)) continue;
iterator.remove();
cfDocenti.remove(CF);
return true;
}
return false;
}
// Metodo 4
public Set<Docente> docentiInRuoloDa4Anni(String codiceMeccanografico) {
// Creazione struttura dati
HashSet<Docente> tmp = new HashSet<Docente>();
// Enumerazione con selezione dei Docenti
Iterator<Docente> it = corpoDocente.iterator();
while (it.hasNext()) {
Docente currentDocente = it.next();
if (!currentDocente.meccanografico().equals(codiceMeccanografico)) continue;
int ar = currentDocente.anniRuolo();
if (ar == 0 || ar >= 4) continue;
tmp.add(currentDocente);
}
return tmp;
}
// Metodo 5
public Map<String, Integer> docentiNonInRuoloPerScuola() {
// Creazione struttura dati
HashMap<String, Integer> tmp = new HashMap<String, Integer>();
// Aggiunta di elementi alla struttura dati
Iterator<Docente> it = corpoDocente.iterator();
while (it.hasNext()) {
Docente currentDocente = it.next();
// Se il docente non è ancora in ruolo
if (currentDocente.anniRuolo() != 0) continue;
String codiceMeccanografico = currentDocente.meccanografico();
if (tmp.get(codiceMeccanografico) == null) tmp.put(codiceMeccanografico, 1);
else tmp.put(codiceMeccanografico, tmp.get(codiceMeccanografico) + 1);
}
return tmp;
}
}
+54
View File
@@ -0,0 +1,54 @@
package totale.p240618;
import java.util.Objects;
public class Docente {
// Variabili
private String CF;
private int anniRuolo;
private String meccanografico;
// Costruttore
public Docente(
String CF,
int anniRuolo,
String meccanografico
) {
this.CF = CF;
this.anniRuolo = anniRuolo;
this.meccanografico = meccanografico;
}
// Getter
public String CF() {
return CF;
}
public int anniRuolo() {
return anniRuolo;
}
public String meccanografico() {
return meccanografico;
}
// Metodo equals
@Override
public boolean equals(Object object) {
if (object == null) return false;
if (object == this) return true;
if (!(object instanceof Docente)) return false;
Docente docente = (Docente) object;
return this.CF.equals(docente.CF);
}
// Metodo hashCode
@Override
public int hashCode() {
return Objects.hash(CF);
}
}
@@ -0,0 +1,79 @@
package totale.p240709;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Comparator;
public class AssegnazioniDocenti {
/*
* Ogni docente è rappresentato, in modo univoco, dal suo codice fiscale e il codice
* meccanografico della scuola.
* <String, String>
*/
private Map<String, String> mappa = new HashMap<String, String>();
// OK
public void insert(
String CF,
String CM
) {
// Null check
if (CF == null || CM == null) throw new NullPointerException();
// Aggiungi o aggiorna il record
mappa.put(CF, CM);
}
// OK
public boolean isDocenteAssigned(String CF) {
// Null check
if (CF == null) throw new NullPointerException();
// Verifica l'esistenza del record
return mappa.containsKey(CF) && mappa.get(CF) != null;
}
// OK
public void removeDocente(String CF) {
// Null check
if (CF == null) throw new NullPointerException();
// Rimuovi
mappa.remove(CF);
}
// OK
public Set<String> getDocentiByCM(String CM) {
// Null check
if (CM == null) throw new NullPointerException();
// Creazione struttura dati
Set<String> tmpSet = new TreeSet<String>();
for (String s : mappa.keySet()) {
String cm = mappa.get(s);
if (cm == null) continue;
if (cm.equals(CM)) tmpSet.add(s);
}
return tmpSet;
}
// OK
public Map<String, Integer> getDocentiForCM() {
// Creazione della struttura dati
Map<String, Integer> tmpMap = new TreeMap<String, Integer>();
// Aggiunta dei dati
for (String s : mappa.keySet()) {
// s = CF
String CM = mappa.get(s);
// Null check
if (CM == null) continue;
// Continuo
if (tmpMap.get(CM) == null) tmpMap.put(CM, 1);
else tmpMap.put(CM, tmpMap.get(CM) + 1);
}
return tmpMap;
}
}