최종: 텍스트 파이프라인 포팅
Coddy의 Kotlin 개발자를 위한 PHP 코스 레슨. 48개 중 47번째.
챌린지
쉬움입력은 문자열 목록입니다. 이 작업에서 제공된 원본 참조 프로그램을 포팅하세요. abbreviations는 빈 문자열을 제거하고, 남은 각 문자열의 첫 번째 ASCII 문자를 가져와 대문자로 바꾼 다음 점으로 연결합니다. 순서를 유지하고, 비어 있지 않은 문자열이 없으면 none을 반환하세요. 모든 입력은 ASCII입니다.
제공된 메인 스크립트는 표준 입력에서 JSON 값 하나를 읽고, 이름이 지정된 공개 API를 검사한 다음 결과를 출력합니다. 출력하지 않도록 모듈 선언을 구현하세요. 런처를 대체하거나 모듈에서 입력을 읽지 마세요. 입력은 명시된 유형과 범위를 만족합니다.
Functions.php를 편집하세요. main.php는 잠긴 진입 파일입니다. 종속성은 상대 파일 이름과 함께 require_once __DIR__를 사용합니다.
Kotlin 참조 프로그램
fun abbreviations(words:List<String>):String {
val parts=words.filter {it.isNotEmpty()}.map {word ->
val first=word[0]
(if(first in 'a'..'z') (first.code-32).toChar() else first).toString()
}
return if(parts.isEmpty()) "none" else parts.joinToString(".")
}
fun main(){println(abbreviations(listOf("red","","green","blue")))}
직접 해보기
<?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";
Kotlin 개발자를 위한 PHP의 모든 레슨
직접 연습해 보세요: 온라인 PHP 컴파일러