最終課題:テキストパイプラインを移植する
Coddyの「GDScript開発者のためのPHP」コースのレッスン 47/48。
チャレンジ
簡単入力は文字列のリストです。このタスクで提供された参照プログラムを移植してください。abbreviations は空文字列を削除し、残った各文字列の最初の ASCII 文字を取得して大文字に変換し、ピリオドで連結します。順序を保ち、空でない文字列がない場合は none を返します。入力はすべて ASCII です。
提供されたメインスクリプトは標準入力から JSON 値を1つ読み取り、指定された公開 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オンラインコンパイラ