Menu
CoddyTech

장바구니: 항목 추가

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

challenge icon

챌린지

쉬움

장바구니 프로젝트의 1단계입니다. 첫 번째 입력 줄은 단계를 선택하며, 이후 줄은 ADD price 또는 REMOVE 1 기반 인덱스입니다. 가격은 0 이상입니다. 이전의 모든 단계와 그 명령어 동작을 유지하세요. 항목을 추가하고 업데이트된 리스트를 반환하도록 add_item을 구현하세요. 1단계는 개수를 반환합니다.

선언된 공개 API를 유지하세요. 보이는 잠긴 드라이버가 이를 직접 검사함과 동시에 solve(lines)를 출력합니다. 입력은 명시된 계약을 충족하며, 모든 연산은 크기가 작고 유한합니다.

작성한 코드는 Basket.R 및 Exercise.R에 들어갑니다. main.R은 의존성을 순서대로 불러오며 제공된 입출력 코드를 포함하고 있으므로 수정할 수 없습니다.

직접 해보기

source("Basket.R")
source("Exercise.R")
lines<-suppressWarnings(readLines(file("stdin"),warn=FALSE))
stage<-as.integer(lines[1])
b<-new_basket();stopifnot(is.list(b),identical(b$prices,numeric(0)))
b<-add_item(b,7);stopifnot(identical(b$prices,7))
if(stage>=2)stopifnot(identical(total(b),7))
if(stage>=3)stopifnot(length(remove_item(b,1)$prices)==0)
if(stage>=4)stopifnot(identical(summary_basket(b),"1:7"))
cat(solve(lines),"\n",sep="")

Kotlin 개발자를 위한 R의 모든 레슨

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