sur->mac
This commit is contained in:
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.
@@ -0,0 +1,43 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import binary_tree.BinaryNode;
|
||||
|
||||
public class BinaryTreeMap {
|
||||
/*
|
||||
* Esercizio 1 - Esame del secondo parziale 8/1/2019
|
||||
* Scrivere un metodo generico statico ContaDuplicati che conta il numero
|
||||
* di oggetti duplicati (non univoci) contenuti in un albero binario.
|
||||
* Il risultato è il conteggio totale degli elementi che risultano duplicati
|
||||
* (non il numero totale di occorrenze, ma il numero di oggetti distinti che
|
||||
* hanno almeno un duplicato)
|
||||
*/
|
||||
public static <T extends Comparable<? super T>> int ContaDuplicati(BinaryNode<T> node) {
|
||||
int dup = 0;
|
||||
TreeMap<T, Integer> mappa = new TreeMap<T, Integer>();
|
||||
ContaDuplicati(node, mappa);
|
||||
|
||||
for (T element : mappa.keySet()) {
|
||||
int occurrency = mappa.get(element);
|
||||
if (occurrency > 1) dup++;
|
||||
}
|
||||
|
||||
return dup;
|
||||
}
|
||||
|
||||
protected static <T> void ContaDuplicati(BinaryNode<T> node, TreeMap<T, Integer> mappa) {
|
||||
if (node == null) return;
|
||||
|
||||
if (node.getLeft() != null) ContaDuplicati(node.getLeft(), mappa);
|
||||
if (node.getRight() != null) ContaDuplicati(node.getRight(), mappa);
|
||||
|
||||
if (node.getData() == null) return;
|
||||
|
||||
T data = node.getData();
|
||||
if (mappa.get(data) == null) mappa.put(data, 1);
|
||||
else mappa.put(data, mappa.get(data) + 1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class CompanyMap {
|
||||
|
||||
/*
|
||||
* EX1
|
||||
* Supponiamo che chi vengano forniti il nome e il numero
|
||||
* di divisione di ciascun dipendente di un'azienda.
|
||||
* Non ci sono nomi duplicati. Vorremmo memorizzare
|
||||
* queste informazioni in ordine alfabetico per nome.
|
||||
*/
|
||||
/*
|
||||
* Spiegazione: si intende adoperare chiaramente un TreeMap.
|
||||
* In modo che vi sia una associazione chiave-valore e un ordinamento basato
|
||||
* sulla chiave.
|
||||
* Chiave: String
|
||||
* Valore: Integer
|
||||
*/
|
||||
|
||||
public static void main(String[] main) {
|
||||
new CompanyMap().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
TreeMap<String, Integer> mappaAssociazione = new TreeMap<String, Integer>();
|
||||
|
||||
mappaAssociazione.put("Rossi Marco", 8);
|
||||
mappaAssociazione.put("Bianchi Luca", 14);
|
||||
mappaAssociazione.put("Esposito Andrea", 6);
|
||||
mappaAssociazione.put("Ferrari Matteo", 6);
|
||||
mappaAssociazione.put("Romano Giulia", 14);
|
||||
mappaAssociazione.put("Ricci Alessia", 6);
|
||||
|
||||
System.out.println("Stampa mappa associazione: ");
|
||||
System.out.println(mappaAssociazione);
|
||||
System.out.println("Numero di dipendenti: " + mappaAssociazione.size());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package jcf_map.exercise;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Dispari<E extends Comparable<? super E>> {
|
||||
|
||||
/*
|
||||
* Esercizio 1 - Esame del 5 settembre 2017
|
||||
*
|
||||
* Una lista si dice dispari se ciascuno dei suoi oggetti appare un numero dispari di volte.
|
||||
* Ad esempio, la lista di interi [20, 5, 5, 20, 20] non è dispari, mentre lo è [5, 20, 5 , 5].
|
||||
* Si assuma una classe Dispari<E> con una sola variabile di instanza private LinkedList<E> myList.
|
||||
* Si implementi un metodo public boolean ordinaDispari() {} interno alla classe,
|
||||
* che restituisce false se myLisy non è dispari, mentre ordina in modo crescente la lista
|
||||
* e restituisce true se myList è dispari;
|
||||
* Si assuma che gli elementi presenti nella lista implementino l'interfaccia Comparable<T>
|
||||
*/
|
||||
|
||||
// Variabile di instanza
|
||||
private LinkedList<E> myList = new LinkedList<E>();
|
||||
|
||||
// Metodo di esercizio
|
||||
public boolean isDispari() {
|
||||
// Se la lista è vuota allora returna dispari
|
||||
if (myList.isEmpty()) return true;
|
||||
|
||||
// Contatore di elementi nella lista
|
||||
HashMap<E, Integer> valori = new HashMap<E, Integer>();
|
||||
|
||||
Iterator<E> it = myList.iterator();
|
||||
while (it.hasNext()) {
|
||||
E current = it.next();
|
||||
if (valori.get(current) == null) valori.put(current, 1);
|
||||
else valori.put(current, valori.get(current) + 1);
|
||||
}
|
||||
|
||||
// Controllo short circuit di disparità
|
||||
for (E chiave : valori.keySet()) {
|
||||
int valore = valori.get(chiave);
|
||||
if (valore % 2 == 0) return false;
|
||||
}
|
||||
|
||||
// Arrivati qui la lista è dispari, manca solo l'ordinamento.
|
||||
Collections.sort(myList);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package list.mylinkedlist;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class MyLinkedList<E> implements List<E> {
|
||||
|
||||
/*
|
||||
* Classe Nodo
|
||||
*/
|
||||
private static class Node<E> {
|
||||
|
||||
// Dati del nodo
|
||||
private E data;
|
||||
|
||||
// Riferimenti elemento precedente e successivo
|
||||
private Node<E> prev;
|
||||
private Node<E> next;
|
||||
|
||||
// Costruttore
|
||||
public Node(E data, Node<E> prev, Node<E> next) {
|
||||
this.data = data;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
private Node<E> tail = null;
|
||||
private int size = 0;
|
||||
|
||||
// Costruttore
|
||||
public MyLinkedList() {}
|
||||
|
||||
// Metodi
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public E getFirst() {
|
||||
if (head == null) throw new NullPointerException();
|
||||
return head.data;
|
||||
}
|
||||
|
||||
public void addFirst(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
head.prev = newNode;
|
||||
newNode.next = head;
|
||||
head = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public E removeFirst() {
|
||||
if (head == null) throw new NoSuchElementException();
|
||||
E tmp = head.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> firstPlace = head;
|
||||
Node<E> secondPlace = head.next;
|
||||
|
||||
head = secondPlace;
|
||||
secondPlace.prev = null;
|
||||
firstPlace.next = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public E getLast() {
|
||||
if (tail == null) throw new NullPointerException();
|
||||
return tail.data;
|
||||
}
|
||||
|
||||
public E removeLast() {
|
||||
if (tail == null) throw new NoSuchElementException();
|
||||
E tmp = tail.data;
|
||||
if (size == 1) {
|
||||
head = tail = null;
|
||||
} else {
|
||||
Node<E> lastNode = tail;
|
||||
Node<E> penultimateNode = tail.prev;
|
||||
|
||||
tail = penultimateNode;
|
||||
penultimateNode.next = null;
|
||||
lastNode.prev = null;
|
||||
}
|
||||
size--;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public void addLast(E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
if (size == 0) {
|
||||
head = tail = newNode;
|
||||
} else {
|
||||
Node<E> oldLastNode = tail;
|
||||
oldLastNode.next = newNode;
|
||||
newNode.prev = oldLastNode;
|
||||
tail = newNode;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean add(E item) {
|
||||
if (item == null) throw new NoSuchElementException();
|
||||
addLast(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, E item) {
|
||||
if (item == null) throw new NullPointerException();
|
||||
if (index < 0 || index > size) throw new IndexOutOfBoundsException();
|
||||
// Se l'inserimento è richiesto nella prima posizione
|
||||
if (index == 0) {
|
||||
addFirst(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nell'ultima posizione
|
||||
if (index == size) {
|
||||
addLast(item);
|
||||
return;
|
||||
}
|
||||
// Se l'inserimento è richiesto nel generico posto i
|
||||
Node<E> newNode = new Node<E>(item, null, null);
|
||||
|
||||
Node<E> prevNode = head;
|
||||
for (int i = 0; i < index - 1; i++) {
|
||||
prevNode = prevNode.next;
|
||||
}
|
||||
|
||||
Node<E> nextNode = prevNode.next;
|
||||
|
||||
prevNode.next = newNode;
|
||||
newNode.prev = prevNode;
|
||||
newNode.next = nextNode;
|
||||
nextNode.prev = newNode;
|
||||
|
||||
size++;
|
||||
}
|
||||
|
||||
public E get(int index) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
return currentNode.data;
|
||||
}
|
||||
|
||||
public E set(int index, E item) {
|
||||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
|
||||
|
||||
Node<E> currentNode = head;
|
||||
for (int i = 0; i < index; i++) {
|
||||
currentNode = currentNode.next;
|
||||
}
|
||||
E oldData = currentNode.data;
|
||||
currentNode.data = item;
|
||||
return oldData;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package parziale.p191108;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Archivio {
|
||||
|
||||
// Variabili di instanza
|
||||
private Map<String, Cliente> archivio = new HashMap<String, Cliente>();
|
||||
|
||||
// Metodi
|
||||
|
||||
// 2. Inserimento nell'archivio
|
||||
/*
|
||||
* Inserimento nell'archio attuale di una nuova associazione,
|
||||
* dati la targa dell'automobile(String) e un riferimento al proprietario
|
||||
* della classe Cliente: se l'automobile è già presente, si aggiorni l'informazione
|
||||
* relativa al proprietario
|
||||
*/
|
||||
public void aggiungi(String targa, Cliente cliente) {
|
||||
if (targa == null || cliente == null) throw new NullPointerException();
|
||||
|
||||
targa = targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.put(targa, cliente);
|
||||
}
|
||||
|
||||
// 3. Cancellazione dall'archivio attuale di un'automobile, data la sua targa
|
||||
public void remove(String targa) {
|
||||
if (targa == null) throw new NullPointerException();
|
||||
|
||||
targa.trim();
|
||||
if (targa.isEmpty()) throw new IllegalArgumentException();
|
||||
|
||||
archivio.remove(targa);
|
||||
}
|
||||
|
||||
// 4. Creazione di un nuovo archivio contenete le automobili selezionate dall'archivio principale aventi il proprietario residente in una data città.
|
||||
public Map<String, Cliente> creaArchivoPerCitta(String citta) {
|
||||
if (citta == null) throw new NullPointerException();
|
||||
|
||||
HashMap<String, Cliente> autoSelezionate = new HashMap<String, Cliente>();
|
||||
|
||||
for (String s : archivio.keySet()) {
|
||||
Cliente currentCliente = archivio.get(s);
|
||||
String currentCitta = currentCliente.cittaResidenza();
|
||||
|
||||
if (currentCitta.equals(citta)) autoSelezionate.put(s, currentCliente);
|
||||
}
|
||||
|
||||
return autoSelezionate;
|
||||
}
|
||||
|
||||
// 5. Ordinamento dell’archivio in modo lessicografico crescente rispetto alla targa delle automobili
|
||||
public Map<String, Cliente> archivioOrdinatoRispettoAllaTarga() {
|
||||
TreeMap<String, Cliente> archivioOrdinato = new TreeMap<String, Cliente>(archivio);
|
||||
return archivioOrdinato;
|
||||
}
|
||||
|
||||
// 6. Stampa su video l'archivio attuale
|
||||
public void stampaArchivio() {
|
||||
System.out.println(archivio);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package parziale.p191108;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Cliente {
|
||||
|
||||
// Variabili di instanza
|
||||
private String nominativo;
|
||||
private String cf;
|
||||
private String cittaResidenza;
|
||||
|
||||
// Costruttore
|
||||
public Cliente(
|
||||
String nominativo,
|
||||
String cf,
|
||||
String cittaResidenza) {
|
||||
this.nominativo = nominativo;
|
||||
this.cf = cf;
|
||||
this.cittaResidenza = cittaResidenza;
|
||||
}
|
||||
|
||||
// Getters
|
||||
public String nominativo() {
|
||||
return nominativo;
|
||||
}
|
||||
|
||||
public String cf() {
|
||||
return cf;
|
||||
}
|
||||
|
||||
public String cittaResidenza() {
|
||||
return cittaResidenza;
|
||||
}
|
||||
|
||||
// Hash
|
||||
public int hashCode() {
|
||||
return Objects.hash(nominativo, cf, cittaResidenza);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user