Menu
CoddyTech

창고: 입고

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

challenge icon

챌린지

쉬움

add를 구현하세요. name이 비어 있거나 quantity가 음수이면 거부하고, 그렇지 않으면 quantity를 누적한 뒤 true를 반환하세요. 0은 허용됩니다. 입력은 stage, command 배열이며, 4단계에서는 price 딕셔너리도 포함됩니다. 명령은 [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))

Java 개발자를 위한 GDScript의 모든 레슨

직접 연습해 보세요: 온라인 GDScript 컴파일러