Prima di tutto, ricordo che sul libro CS awesome ci sono 5 practice units:
Practice units: 11, 12, 13, 14, 15
Contengono parecchie domande ed esercizi, in particolare penso troverete utili le domande sulle classi. Purtroppo sono mischiate, quindi non ha molto senso che vi metta link specifici, dovrete cercarle voi.
Riguardo il nostro esame, abbiamo trattato la maggior parte del libro eccetto la ricorsione e per le classi Java richiederò solo qualche semplice esercizio di modellazione come quelli visti in classe.
Qui di seguito riporto inoltre esercizi che ho proposto agli orali più diversi altri, in particolare per matrici e hashmaps. Non è strettamente necessario farli tutti e probabilmente non ne avrete il tempo, consiglio di farne un po' per tipo in base alle aree in cui avete più bisogno di approfondire.
Gli esercizi sono divisi in sezioni:
Edabit - Pages in a Book
Characterland is ruled with the iron fist by the Dukes of Stringards. The towns managed by them are monodimensional, and can be represented as a string: the host dukes d
, lords s
, vassals v
and peasants p
. To separate the various social circles from improper mingling, some walls |mm|
have been erected.
Over time the dukes family has expanded and alas ruthless feuds have happened. According to the number of town people to the left of the dukes or to the right, a corresponding number of royal members to the left or to the right receives support to do their power games. A member of the dukes palace who receives support becomes uppercase. Each character 'p'
, 'v'
or 's'
contributes support (but not the walls). The royal members who are not reached by support are slaughtered by their siblings, and substituted with a Latin Cross Unicode ✝
d
is always present, and it is uniqueExample - given:
String town = "ppp|mm|vv|mm|v|s|mm|dddddddddddddddddddddddd|mm|ss|mm|vvvvv|mm|pppp";
After your code, it must print:
Members of the royal family:24
left:7
right:11
After the deadly struggle, the new town is
ppp|mm|vv|mm|v|s|mm|DDDDDDD✝✝✝✝✝✝DDDDDDDDDDD|mm|ss|mm|vvvvv|mm|pppp
String town = "ppp|mm|vv|mm|v|s|mm|dddddddddddddddddddddddd|mm|ss|mm|vvvvv|mm|pppp";
// result: "ppp|mm|vv|mm|v|s|mm|DDDDDDD✝✝✝✝✝✝DDDDDDDDDDD|mm|ss|mm|vvvvv|mm|pppp" // tot:24 sx:7 dx:11
//town = "ppp|mm|ppp|mm|vv|mm|ss|mm|dddddddddddddddddddd|mm|ss|mm|mm|s|v|mm|p|p|";
// result: "ppp|mm|ppp|mm|vv|mm|ss|mm|DDDDDDDDDD✝✝✝✝DDDDDD|mm|ss|mm|mm|s|v|mm|p|p|" // tot:20 sx:10 dx:6
Given a a list house
as a list of strings nxm
and a character list bricks
of n+1
elements, RETURN a NEW list obtained by alternating rows of house
with characters taken from bricks
import java.util.List;
public static List<String> house(List<String> mura, List<String> mattonelle){
throw new RuntimeException("TODO IMPLEMENT ME!");
}
List<String> m4 = List.of("feab",
"acgf",
"bcdf");
// house(m4, List.of("^","-","_","="))
/*
should produce
[" ^^^^ ",
"|feab|",
"|----|",
"|acgf|",
"|____|",
"|bcdf|",
"|====|"]
*/
/** Assume u and v are two vectors represented as lists,
RETURN a NEW list containing the vectorial sum of u and v
- if the length of u is different from length of v, raise a RuntimeException
- if you encounter nulls, raise RuntimeException (could be many ...)
*/
public static List<Integer> sumvec(List<Integer> u, List<Integer> v){
throw new RuntimeException("TODO IMPLEMENT ME!");
}
// sumvec(List.of(4,2,6,3), List.of(8,1,4,2)); // [12, 3, 10, 5]
// sumvec(List.of(7,5), List.of(8,1,4,2)); // exception!
// sumvec(null, List.of(8,1,4,2)); // exception!
// sumvec(List.of(4,2,6,3), null); // exception!
// sumvec(null, null); // exception!
ArrayList<Integer> arr = new ArrayList();
arr.add(2);
arr.add(null);
//sumvec(arr, List.of(7,9)); // exception!
//sumvec(List.of(8,5), arr); // exception!
//sumvec(arr, arr); // exception!
true
RETURN true
if provided list has the first three elements ordered in ascending order, otherwise return false
.
• if the list has less than 3 elements, return false
public static boolean ord3(List<Integer> lista)
ord3([5]) -> false
ord3([4,7]) -> false
ord3([4,6,9]) -> true
ord3([4,9,7]) -> false
ord3([9,5,7]) -> false
ord3([4,8,9,1,5]) -> true # first 3 are increasing
ord3([9,4,8,10,13]) -> false # first 3 are NOT increasing
/*
Given a string word, RETURN a NEW string with the characters progressively replicated.
*/
public static String tante_lettere(String parola){
throw new RuntimeException("TODO IMPLEMENT ME!");
}
/*
tante_lettere("") -> ""
tante_lettere("a") -> "a";
tante_lettere("abc") -> "abbccc";
tante_lettere("GOAL") -> "GOOAAALLLL";
tante_lettere("abaco") -> "abbaaaccccooooo";
*/
https://edabit.com/challenge/33tRK98geLPcf73PF
Create a function that transforms sentences ending with multiple question marks ? or exclamation marks ! into a sentence sith single sequences of exclamations
Examples
noYelling("What went wrong?????????") ➞ "What went wrong?"
noYelling("Oh my goodness!!!") ➞ "Oh my goodness!"
noYelling("I just!!! can!!! not!!! believe!!! it!!!") ➞ "I just! can! not! believe! it!"
// change repeating punctuation inside the sentence (NOTE: the requirement is different from Edabit)
noYelling("Oh my goodness!") ➞ "Oh my goodness!"
// Do not change sentences where there exists only one or zero exclamation marks/question marks.
noYelling("I just cannot believe it.") ➞ "I just cannot believe it."
Notes
Don't worry about mixed punctuation (no cases that end in something like ?!??!).
Keep sentences that do not have question/exclamation marks the same.
Given two integer numbers n
and m
, with m
a multiple of 3, RETURN a list of n
strings of length m
having in the strings the digits from 0
to 2
in three vertical strips.
public static List<String> flag(int n, int m)
input:
flag(5,12)
output:
["000011112222",
"000011112222",
"000011112222",
"000011112222",
"000011112222"]
Create a function that returns true if the first array can be nested inside the second.
arr1 can be nested inside arr2 if:
arr1's min is greater than arr2's min. arr1's max is less than arr2's max.
Examples
canNest([1, 2, 3, 4], [0, 6]) ➞ true
canNest([3, 1], [4, 0]) ➞ true
canNest([9, 9, 8], [8, 9]) ➞ false
canNest([1, 2, 3, 4], [2, 3]) ➞ false
Note: the strict inequality (see example #3).
✪✪ As a skilled breakdancer, you're given music
as a list of sounds. You will have to perform a couple of dances:
3
sounds 'pa'
, then you will shout BREAKDANCE!
.3
sounds 'pa'
, then you will shout BREAKDANCE!
Example - given:
List<String> music = List.of('unz','pa','pa','tud','unz','pa','pa','tud','unz','boom','boom','tud')
Prints:
unz
pa
pa
tud
unz
pa
BREAKDANCE!
tud
boom
boom
unz
tud
pa
pa
unz
tud
pa
BREAKDANCE!
List<String> music = List.of("unz","pa","pa","tud","unz","pa","pa","tud","unz","boom","boom","tud");
// write here
✪✪ The two outlaws Carson and Butch agreed to bury a treasure in the jolly town of Tombstone, ma now each of them wants to take back the treasure without sharing anything with the partner.
road
from Santa Fe
until Tombstone
which we represent as a list of stringsbutch
and carson
to represent where the outlaws are on the roadWrite some code which prints the run and terminates as soon as one them arrives to the last city, telling who got the treasure.
In the case both outlaws arrive to the last city at the same time, prints Final duel in Tombstone!
your code must work for any road
and initial position carson
and butch
Example - 1 given:
# 0 1 2 3 4 5
List<String> road = List.of("Santa Fe","Denver","Dodge City", "Silverton", "Agua Caliente", "Tombstone");
int carson = 3, butch = 0;
it must print:
Carson starts from Silverton
Butch starts from Santa Fe
Carson reaches Agua Caliente
Butch reaches Dodge City
Carson reaches Tombstone
Butch reaches Agua Caliente
Carson takes the treasure in Tombstone !
Example 2 - given:
# 0 1 2 3 4 5
List<String> road = List.of("Santa Fe","Denver","Dodge City", "Silverton", "Agua Caliente", "Tombstone");
int carson = 3, butch = 2;
it must print:
Carson starts from Silverton
Butch starts from Dodge City
Carson reaches Agua Caliente
Butch reaches Agua Caliente
Carson reaches Tombstone
Butch reaches Tombstone
Final duel in Tombstone !
List<String> road = List.of("Santa Fe","Denver","Dodge City", "Silverton", "Agua Caliente", "Tombstone");
int carson = 3, butch = 0; // Carson takes the treasure in Tombstone !
//carson = 0; butch = 0; // Butch takes the treasure in Tombstone !
//carson = 3; butch = 2; // Final duel in Tombstone !
// write here
If you don't know how to do them, notice there is often on Editorial tab with an article explaining them.
https://edabit.com/challenge/bpqfCQ7zumf5Ep24Z
ATTENZIONE: ci sono due modi per farlo: quello semplice è contare le frequenze con le hashmap ma richiede più memoria. Un modo alternativo più efficiente, ma più difficile, è usare solo con quattro variabili intere.
5 3 3 3
oddOneOut(["silly", "mom", "let", "the"]) ➞ true
6 5 6
oddOneOut(["swanky", "rhino", "moment"]) ➞ true
3 4 5
oddOneOut(["the", "them", "theme"]) ➞ false
4 2 2 4
oddOneOut(["very", "to", "an", "some"]) ➞ false
1 2
oddOneOut(["a", "bc"]) ➞ false
3 3 3 3 5
oddOneOut(["dir", "dac", "inc", "tra", "mondo"]) ➞ false
Notes