Menu
CoddyTech

Ripasso valutato dei contratti

Lezione 42 di 48 del corso Rust per sviluppatori C di Coddy.

challenge icon

Sfida

Facile

Definisci scorer::Scorer con score(&self, word: &str) -> usize. In moduli separati, implementa length::Length come struct unit che calcola il numero di byte e vowels::Vowels come struct unit che conta le vocali ASCII, maiuscole o minuscole. ranking::best<S: Scorer>(words: &[String], scorer: &S) -> Option<String> restituisce una copia di proprietà della parola con il punteggio più alto; in caso di parità, sceglie la prima parola nell'input e, se la slice è vuota, restituisce None. Chiama lo scorer fornito per ogni parola, invece di presupporne il tipo concreto. solve legge la modalità length o vowels, seguita da parole ASCII. Restituisci la parola migliore o empty quando non ci sono parole. L'input contiene al massimo 100 parole, ciascuna di massimo 100 byte.

Implementa le API esportate richieste e restituisci il risultato di exercise::solve(input: &str) -> String senza un newline finale. Non stampare nel codice modificabile. Il driver bloccato verifica le API richieste in modo indipendente, legge tutto lo stdin e stampa il testo restituito con un newline finale.

Il tuo codice va inserito in exercise.rs, scorer.rs, length.rs, vowels.rs, ranking.rs. main.rs contiene il codice fornito per input/output e non può essere modificato.

Provalo tu

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod scorer;
mod length;
mod vowels;
mod ranking;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // INIZIO DEI TEST DELL'API PUBBLICA
use scorer::Scorer;
assert_eq!(length::Length.score("ridge"), 5); assert_eq!(length::Length.score(""), 0);
assert_eq!(vowels::Vowels.score("AEiouY"), 5); assert_eq!(vowels::Vowels.score("rhythm"), 0);
let words = vec!["north".to_string(), "east".to_string(), "south".to_string()];
assert_eq!(ranking::best(&words, &length::Length), Some("north".to_string()));
assert_eq!(ranking::best(&words, &vowels::Vowels), Some("east".to_string()));
struct PreferLastLetter;
impl scorer::Scorer for PreferLastLetter { fn score(&self, w: &str) -> usize { w.bytes().last().unwrap_or(0) as usize } }
assert_eq!(ranking::best(&words, &PreferLastLetter), Some("east".to_string()));
assert_eq!(ranking::best(&[], &PreferLastLetter), None);
let zeros = vec!["dry".to_string(), "sky".to_string()];
assert_eq!(ranking::best(&zeros, &vowels::Vowels), Some("dry".to_string()));
assert_eq!(ranking::best(&words, &length::Length), Some("north".to_string()));
    // FINE DEI TEST DELL'API PUBBLICA
    println!("{}", exercise::solve(&input));
}

Tutte le lezioni di Rust per sviluppatori C

Esercitati da solo: Compilatore Rust online