Menu
CoddyTech

Finale: portare una pipeline di testo

Lezione 47 di 48 del corso PHP per sviluppatori Rust di Coddy.

challenge icon

Sfida

Facile

L'input è un elenco di stringhe. Porta il programma di riferimento sorgente fornito in questa attività: abbreviations rimuove le stringhe vuote, prende il primo carattere ASCII di ogni stringa rimanente, lo converte in maiuscolo e unisce i caratteri con dei punti. Mantieni l'ordine; restituisci none se non ci sono stringhe non vuote. Tutti i dati di input sono ASCII.

Lo script main fornito legge un valore JSON dallo standard input, verifica le API pubbliche indicate e stampa il risultato. Implementa le dichiarazioni del modulo senza stampare; non sostituire il launcher né leggere l'input da un modulo. L'input rispetta i tipi e i limiti indicati.

Modifica Functions.php. main.php è il file di ingresso bloccato. Le dipendenze usano require_once __DIR__ con un nome di file relativo.

Programma di riferimento Rust

fn abbreviations(words:&[String])->String {
    let parts:Vec<String>=words.iter().filter_map(|word| {
        word.as_bytes().first().map(|first| char::from(first.to_ascii_uppercase()).to_string())
    }).collect();
    if parts.is_empty(){"none".to_owned()}else{parts.join(".")}
}
fn main(){
    let words:Vec<String>=["red","","green","blue"].iter().map(|word|(*word).to_owned()).collect();
    println!("{}",abbreviations(&words));
}

Provalo tu

<?php
declare(strict_types=1);
require_once __DIR__."/Functions.php";
function ensure(bool $ok,string $message):void{if(!$ok)throw new RuntimeException("API: ".$message);}

$input=json_decode(trim(stream_get_contents(STDIN)),true,512,JSON_THROW_ON_ERROR);
ensure(abbreviations(["a","","b"])==="A.B" && abbreviations([])==="none","port pipeline");
echo abbreviations($input),"\n";

Tutte le lezioni di PHP per sviluppatori Rust

Esercitati da solo: Compilatore PHP online