장바구니: 항목 삭제
Coddy의 Rust 개발자를 위한 R 코스 레슨. 48개 중 45번째.
챌린지
쉬움바구니 프로젝트의 3단계입니다. 첫 번째 입력 줄은 단계를 선택하고, 그다음 줄들은 ADD 가격 또는 REMOVE 1부터 시작하는 인덱스입니다. 가격은 음수가 아닙니다. 이전 단계와 각 단계의 명령 동작을 모두 유지하세요. remove_item을 구현하고, 유효하지 않은 인덱스는 무시하세요. 3단계에서는 남은 가격의 합을 반환합니다.
선언된 공개 API를 유지하세요. 화면에 보이는 잠금된 드라이버는 solve(lines)를 출력하는 것뿐 아니라 API도 직접 확인합니다. 입력은 명시된 조건을 만족하며, 모든 산술 연산은 값이 작고 유한합니다.
코드는 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="")
Rust 개발자를 위한 R의 모든 레슨
직접 연습해 보세요: 온라인 R 컴파일러