장바구니: 요약
Coddy의 Kotlin 개발자를 위한 R 코스 레슨. 48개 중 46번째.
챌린지
쉬움장바구니 프로젝트의 Stage 4입니다. 첫 번째 입력 줄은 단계를 선택하며, 그 다음 줄들은 ADD price 또는 REMOVE 1 기반 인덱스입니다. 가격은 0 이상입니다. 이전의 모든 단계와 해당 명령 동작을 유지하세요. summary_basket을 구현하세요. stage4는 count:sum을 반환합니다.
선언된 공개 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 컴파일러