Menu
CoddyTech

最终挑战:移植文本处理流程

Coddy 面向 GDScript 开发者的 PHP 课程的第 47 课,共 48 课。

challenge icon

挑战

简单

输入是一个字符串列表。请移植此任务提供的源参考程序:abbreviations 会移除空字符串,获取其余每个字符串的第一个 ASCII 字符,将其转换为大写,然后用点连接。保留顺序;如果没有非空字符串,则返回 none。所有输入均为 ASCII。

提供的主脚本会从标准输入读取一个 JSON 值,检查指定的公共 API,并打印结果。实现模块声明时不要打印内容;不要替换启动器,也不要从模块读取输入。输入符合所述类型和范围。

编辑 Functions.php。main.php 是锁定的入口文件。依赖项使用带相对文件名的 require_once __DIR__。

GDScript 参考程序

extends SceneTree
# Byte arrays represent ASCII strings, including a possible zero byte.
func abbreviations(words: Array) -> PackedByteArray:
    var output = PackedByteArray()
    var found = false
    for word in words:
        if word.is_empty():
            continue
        if found:
            output.append(46)
        var first = word[0]
        if first >= 97 and first <= 122:
            first -= 32
        output.append(first)
        found = true
    return output if found else "none".to_ascii_buffer()
func _init():
    var words = []
    for word in ["red", "", "green", "blue"]:
        words.append(word.to_ascii_buffer())
    print(abbreviations(words).get_string_from_ascii())
    quit()

亲手试试

<?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";

面向 GDScript 开发者的 PHP中的所有课

自己练习: PHP 在线编译器