Menu
CoddyTech

购物篮:移除商品

Coddy 面向 Python 开发者的 R 语言 课程的第 45 课,共 48 课。

challenge icon

挑战

简单

这是购物篮项目的第 3 阶段。第一行输入用于选择阶段;后续每行是 ADD 价格或 REMOVE 从 1 开始的索引。价格均为非负数。保留之前所有阶段及其命令行为。实现 remove_item;忽略无效索引;阶段 3 返回剩余价格总和。

保留已声明的公共 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="")

面向 Python 开发者的 R 语言中的所有课

自己练习: R 在线编译器