Menu
CoddyTech

Finale: portare una pipeline di testo

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

challenge icon

Sfida

Facile

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

Lo script principale fornito legge un valore JSON dall'input standard, verifica le API pubbliche indicate e stampa il risultato. Implementa le dichiarazioni del modulo senza stampare; non sostituire il launcher e non 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 R

# Raw vectors represent ASCII strings, including possible zero bytes.
abbreviations <- function(words) {
    output <- raw()
    found <- FALSE
    for(word in words) {
        if(length(word)==0L) next
        if(found) output <- c(output,as.raw(46))
        first <- as.integer(word[1L])
        if(first>=97L && first<=122L) first <- first-32L
        output <- c(output,as.raw(first))
        found <- TRUE
    }
    if(found) output else charToRaw("none")
}
words <- lapply(c("red","","green","blue"),charToRaw)
cat(rawToChar(abbreviations(words)),"\n",sep="")

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 R

Esercitati da solo: Compilatore PHP online