Quali casi di test devono essere considerati per il metodo join? Quali ottimizzazioni possono essere aggiunte?
Kotlin
// prenotazione(sedile_id, user_id) PostgreSQL Begin Transaction(read_commited) seat = select * from seats where id = seat_id for update if(seat == null || seat.status == "booked") rollback return error("Posto non disponibile") insert into bookings (user_id, seat_id, status, created_at) values (user_id, ...) update seats set status = 'booked' where id = seat_id commit return success()
Perché in Map la chiave è la parentesi di chiusura e il valore quella di apertura? Cosa cambierebbe se si invertisse? Qual è la complessità di containsValue rispetto a get per chiave?
/*Dato una stringa, è necessario implementare una funzione per verificare la correttezza dell'indicazione delle parentesi. Ogni parentesi aperta deve corrispondere a una parentesi chiusa. Le parentesi aperte e chiuse devono essere in ordine corretto. Tipi di parentesi: "{", "}", "(", ")", "[", "]". Input: "([]){}[(()())()]" Risultato: true Input: "[(])" Risultato: false Perché le parentesi 2 e 3 sono indicate in modo errato, la chiusura ']' si presenta prima della chiusura '(' */ public boolean check(String s){ Deque<Character> stack = new ArrayDeque<>(); Map<Character, Character> map = Map.of( ')', '(', ']', '[', '}', '{' ); // ((( for( char c : s.toCharArray()){ if(map.containsValue(c)){ stack.push(c); } else if(map.containsKey(c)){ if( stack.isEmpty() || stack.pop() != map.get(c)){ return false; } } } return stack.isEmpty(); }
/* SELEZIONA sinistra.K, sinistra.V1, destra.V2 DA sinistra JOIN destra ON sinistra.K = destra.K; */ public <K extends Comparable<K>, V1, V2> List<Triple<K, V1, V2>> join(List<Pair<K, V1>> sinistra, List<Pair<K, V2>> destra) { return null; } private static class Pair<A, B> { private A primo; private B secondo; Pair(A primo, B secondo) { this.primo = primo; this.secondo = secondo; } } private static class Triple<A, B, C> { private A primo; private B secondo; private C terzo; Triple(A primo, B secondo, C terzo) { this.primo = primo; this.secondo = secondo; this.terzo = terzo; } }