Menu
CoddyTech

최종 과제: 텍스트 파이프라인 포팅하기

Coddy의 GDScript 개발자를 위한 PHP 코스 레슨. 48개 중 47번째.

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 컴파일러