Menu
CoddyTech

Riepilogo della valutazione dei contratti

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

challenge icon

Sfida

Facile

Definisci scorer::Scorer con score(&self, word: &str) -> usize. In moduli separati, implementa length::Length come struttura unitaria che calcola il numero di byte e vowels::Vowels come struttura unitaria che conta le vocali ASCII in entrambe le maiuscole e 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 una slice vuota restituisce None. Chiama lo scorer fornito per ogni parola, invece di presumerne il tipo concreto. solve legge la modalità length o vowels, seguita da parole ASCII. Restituisci la parola migliore oppure empty se non ci sono parole. L'input contiene al massimo 100 parole, ciascuna di al 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 separatamente le API richieste, legge tutto lo stdin e stampa il testo restituito con un newline finale.

Il tuo codice va 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 DELLE VERIFICHE 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 DELLE VERIFICHE DELL'API PUBBLICA
    println!("{}", exercise::solve(&input));
}

Tutte le lezioni di Rust per sviluppatori GDScript

Esercitati da solo: Compilatore Rust online