Menu
CoddyTech

购物篮:移除商品

Coddy 面向 Kotlin 开发者的 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="")

面向 Kotlin 开发者的 R中的所有课

自己练习: R 在线编译器