diff --git a/asdl/bin/binary_tree/LinkedBinaryTree.class b/asdl/bin/binary_tree/LinkedBinaryTree.class index 4809778..d0a6515 100644 Binary files a/asdl/bin/binary_tree/LinkedBinaryTree.class and b/asdl/bin/binary_tree/LinkedBinaryTree.class differ diff --git a/asdl/bin/network/network/Network$BreadthFirstIterator.class b/asdl/bin/network/network/Network$BreadthFirstIterator.class index ec0923a..1373616 100644 Binary files a/asdl/bin/network/network/Network$BreadthFirstIterator.class and b/asdl/bin/network/network/Network$BreadthFirstIterator.class differ diff --git a/asdl/bin/network/network/Network$DepthFirstIterator.class b/asdl/bin/network/network/Network$DepthFirstIterator.class index f091a57..1a95184 100644 Binary files a/asdl/bin/network/network/Network$DepthFirstIterator.class and b/asdl/bin/network/network/Network$DepthFirstIterator.class differ diff --git a/asdl/bin/network/network/Network$NetworkIterator.class b/asdl/bin/network/network/Network$NetworkIterator.class index 29b534e..66276f8 100644 Binary files a/asdl/bin/network/network/Network$NetworkIterator.class and b/asdl/bin/network/network/Network$NetworkIterator.class differ diff --git a/asdl/bin/network/network/Network.class b/asdl/bin/network/network/Network.class index ac6f84c..c7c5019 100644 Binary files a/asdl/bin/network/network/Network.class and b/asdl/bin/network/network/Network.class differ diff --git a/asdl/bin/totale/p160726/Corriere$1.class b/asdl/bin/totale/p160726/Corriere$1.class new file mode 100644 index 0000000..6e227e9 Binary files /dev/null and b/asdl/bin/totale/p160726/Corriere$1.class differ diff --git a/asdl/bin/totale/p160726/Corriere.class b/asdl/bin/totale/p160726/Corriere.class new file mode 100644 index 0000000..380a399 Binary files /dev/null and b/asdl/bin/totale/p160726/Corriere.class differ diff --git a/asdl/bin/totale/p160726/Spedizione.class b/asdl/bin/totale/p160726/Spedizione.class new file mode 100644 index 0000000..6971d4d Binary files /dev/null and b/asdl/bin/totale/p160726/Spedizione.class differ diff --git a/asdl/bin/totale/p240618/CorpoDocente$1.class b/asdl/bin/totale/p240618/CorpoDocente$1.class new file mode 100644 index 0000000..586cca4 Binary files /dev/null and b/asdl/bin/totale/p240618/CorpoDocente$1.class differ diff --git a/asdl/bin/totale/p240618/CorpoDocente.class b/asdl/bin/totale/p240618/CorpoDocente.class new file mode 100644 index 0000000..ffb50f6 Binary files /dev/null and b/asdl/bin/totale/p240618/CorpoDocente.class differ diff --git a/asdl/bin/totale/p240618/Docente.class b/asdl/bin/totale/p240618/Docente.class new file mode 100644 index 0000000..28ae5c6 Binary files /dev/null and b/asdl/bin/totale/p240618/Docente.class differ diff --git a/asdl/bin/totale/p240709/AssegnazioniDocenti.class b/asdl/bin/totale/p240709/AssegnazioniDocenti.class new file mode 100644 index 0000000..2e9a109 Binary files /dev/null and b/asdl/bin/totale/p240709/AssegnazioniDocenti.class differ diff --git a/asdl/src/binary_tree/LinkedBinaryTree.java b/asdl/src/binary_tree/LinkedBinaryTree.java index 1421838..3570369 100644 --- a/asdl/src/binary_tree/LinkedBinaryTree.java +++ b/asdl/src/binary_tree/LinkedBinaryTree.java @@ -615,6 +615,91 @@ public class LinkedBinaryTree implements BinaryTree{ return Integer.compare(currentData, leftData + rightData) == 0; } - + public boolean esisteNodoConFigliUguali() { + if (root == null) return false; + return esisteNodoConFigliUguali(root); + } + + public boolean esisteNodoConFigliUguali(BinaryNode 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 boolean TwinChildren(BinaryNode 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 boolean TwinChildren(BinaryNode 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; + } } diff --git a/asdl/src/network/network/Network.java b/asdl/src/network/network/Network.java index 1a56e1b..6effddb 100644 --- a/asdl/src/network/network/Network.java +++ b/asdl/src/network/network/Network.java @@ -2,6 +2,7 @@ package network.network; import network.Graph; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -53,6 +54,58 @@ public class Network> 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 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 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 l’arco orientato x→y, avente come peso un + * valore double -p se xy, dove p ε[0,1) è generato con il metodo + * Math.random(). + */ + public Network(ArrayList L1, ArrayList 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 public boolean equals(Object object) { diff --git a/asdl/src/totale/p160726/Corriere.java b/asdl/src/totale/p160726/Corriere.java new file mode 100644 index 0000000..1e62d73 --- /dev/null +++ b/asdl/src/totale/p160726/Corriere.java @@ -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 currentSpedizioni = new TreeMap(); + private TreeSet fixedSpedizioni = new TreeSet(); + + // 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 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 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 spedizioniPerPesoECodice() { + List tmp = new ArrayList(currentSpedizioni.keySet()); + Comparator cmp = new Comparator() { + @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 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 numeroSpedizioniInGestionePerData() { + // Creazione della struttura dati + Map tmp = new HashMap(); + // 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; + } + +} diff --git a/asdl/src/totale/p160726/Spedizione.java b/asdl/src/totale/p160726/Spedizione.java new file mode 100644 index 0000000..338de36 --- /dev/null +++ b/asdl/src/totale/p160726/Spedizione.java @@ -0,0 +1,53 @@ +package totale.p160726; + +public class Spedizione implements Comparable{ + + // 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); + } + +} diff --git a/asdl/src/totale/p240618/CorpoDocente.java b/asdl/src/totale/p240618/CorpoDocente.java new file mode 100644 index 0000000..084e24f --- /dev/null +++ b/asdl/src/totale/p240618/CorpoDocente.java @@ -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 corpoDocente = new TreeSet(new Comparator() { + @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 cfDocenti = new HashSet(); + + // 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 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 docentiInRuoloDa4Anni(String codiceMeccanografico) { + // Creazione struttura dati + HashSet tmp = new HashSet(); + // Enumerazione con selezione dei Docenti + Iterator 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 docentiNonInRuoloPerScuola() { + // Creazione struttura dati + HashMap tmp = new HashMap(); + // Aggiunta di elementi alla struttura dati + Iterator 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; + } + +} diff --git a/asdl/src/totale/p240618/Docente.java b/asdl/src/totale/p240618/Docente.java new file mode 100644 index 0000000..ac92de5 --- /dev/null +++ b/asdl/src/totale/p240618/Docente.java @@ -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); + } + +} diff --git a/asdl/src/totale/p240709/AssegnazioniDocenti.java b/asdl/src/totale/p240709/AssegnazioniDocenti.java new file mode 100644 index 0000000..a835175 --- /dev/null +++ b/asdl/src/totale/p240709/AssegnazioniDocenti.java @@ -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. + * + */ + private Map mappa = new HashMap(); + + // 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 getDocentiByCM(String CM) { + // Null check + if (CM == null) throw new NullPointerException(); + // Creazione struttura dati + Set tmpSet = new TreeSet(); + 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 getDocentiForCM() { + // Creazione della struttura dati + Map tmpMap = new TreeMap(); + // 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; + } + +} \ No newline at end of file