获取数字列表的函数。该函数应返回该列表的前x个元素,以使新元素的总和小于42。
答案 0 :(得分:0)
list <- list(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10)
list
get_elements_func <- function(list, max){
sum_vector <- 0
for(i in 1:length(list)){
sum_vector <- rbind(sum_vector, list[[i]])
if(sum(sum_vector) >= max){
i <- i - 1
break
}
}
return(list[1:i])
}
get_elements_func(list, 42)