창고: 보고서
Coddy의 Swift 개발자를 위한 GDScript 코스 레슨. 48개 중 44번째.
챌린지
쉬움add는 그대로 둡니다. report를 구현하세요. ASCII 기준으로 정렬된 name=quantity 쌍을 쉼표로 구분해 표시하며, 비어 있는 창고는 none을 반환합니다. 입력은 stage, 명령 배열, 그리고 4단계에서는 가격 딕셔너리입니다. 명령은 [add 또는 take,name,quantity]입니다. solve는 현재 단계에서 사용할 수 있는 연산만 디스패치합니다. 프로젝트 전체에서 레이아웃과 잠긴 main은 변경하지 않습니다. 새로 요구된 StockDepot 메서드만 완성하세요.
입력은 stdin에 전달되는 하나의 JSON 배열입니다. 잠긴 main은 이를 파싱하고 공개 API를 호출한 다음, 반환된 텍스트 앞에 result=를 붙이고 개행 문자를 출력합니다. 어떤 헬퍼 함수도 출력하면 안 됩니다. 입력 사례를 처리하기 전에 독립적인 고정 프로브로 필수 API 동작을 확인합니다. 프로브는 구현 스타일을 강제하지 않습니다.
코드는 stock_depot.gd와 exercise.gd에 작성합니다. main.gd에는 제공된 입력/출력 코드가 있으며 수정할 수 없습니다. 각 class_name 타입은 지정된 파일에 두세요.
직접 해보기
extends Node
func contract_ok(data: Array) -> bool:
var stage: int = int(data[0])
var depot: StockDepot = StockDepot.new()
if depot.add("",2) or depot.add("p",-1): return false
if not depot.add("p",3) or not depot.add("p",2): return false
if stage >= 2 and depot.report() != "p=5": return false
if stage >= 3:
if depot.take("p",6) or depot.take("q",0) or depot.take("p",-1): return false
if depot.report() != "p=5" or not depot.take("p",2) or depot.report() != "p=3": return false
if stage >= 4 and depot.worth({"p":4}) != 12: return false
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
Swift 개발자를 위한 GDScript의 모든 레슨
직접 연습해 보세요: 온라인 GDScript 컴파일러