Corso di Raku / Avanzato / Altro sui tipi predefiniti / Set, bag e mix / Exercises / Contare in un bag
Soluzione: Contare in un bag
Ecco una possibile soluzione.
Codice
my $words = bag('the cat sat on the mat'.words);
say $words<the>;
say $words.total;🦋 Puoi trovare il codice sorgente nel file bag-count.raku.
Output
2
6Commenti
'the cat sat on the mat'.wordsproduce le sei parole ebagconta ciascuna di esse. Il subscript<the>restituisce quante voltetheè stato trovato — due.Il metodo
totalsomma tutti i conteggi, che corrisponde al numero di parole inserite nel bag —6.
Navigazione del corso
← Contare in un bag | Sequenze →