如何检查一个序列是否是“几乎递增序列”?在 R 中?
当我们可以从序列中恰好删除一个元素并得到严格递增序列(即 a0 < a1 < ... < an)时,序列(例如 c(1,2,3,4))几乎是递增的。我正在尝试找到一种方法来检查序列是否几乎在增加。如果是,我想返回TRUE;如果不是我想输出FALSE。我已经到目前为止:
solution <- function(sequence) {
sequence1 <- unlist(sequence)
if (length(sequence1) == 1) {
next
}
count <- 0
for (i in (length(sequence1) - 1)) {
if (sequence1[i + 1] > sequence1[i]) {
next
} else if (((sequence1[i + 2] > sequence1[i]) & count == 0) & i !=
length(sequence1)-1) {
sequence1 <- sequence1[- (i + 1)]
count <- count + 1
} else if ((sequence1[i + 1] > sequence1[i - 1]) & count == 0 & i != 1) {
sequence1 <- sequence1[-i]
count <- count + 1
} else {
return(FALSE)
}
}
return(TRUE)
}
我使用了 unlist() 因为 codesignal 由于某种原因不接受您在函数中引用函数参数。这适用于某些序列:solution(c(4,1,5)) 正确返回 TRUE。它对其他人不起作用:solution(c(1, 1, 1, 2, 3)) 错误地返回 TRUE。 Solution(c(2,1,2,1)) 正确返回 FALSE,但 Solution(c(1,2,1,2)) 错误返回 TRUE。我对正在发生的事情失去了控制。我想知道是否有人能发现什么?
澄清:我的代码的基本思想是迭代序列并为每个元素检查其右邻居是否是更大的数字。如果不是,那么我们有两个选择:去掉 i 或去掉 i+1,所以我依次检查这些。由于我们只能进行一项更改,因此我添加了一个条件:如果计数为 1,则我们跳到完成。另外,如果索引是 1 那么我们不能检查 i-1,如果索引是 length(sequence)-1,那么我们不能检查 i+2,所以我添加了这些条件以确保如果合适的话,我的代码会跳到其他选项。
A sequence (e.g. c(1,2,3,4)) is almost increasing when we can remove exactly one element from the sequence and get a strictly increasing sequence (i.e. a0 < a1 < ... < an). I'm trying to find a way to check whether a sequence is almost increasing. If it is, I want to return TRUE; if it isn't I want to output FALSE. I've got this far:
solution <- function(sequence) {
sequence1 <- unlist(sequence)
if (length(sequence1) == 1) {
next
}
count <- 0
for (i in (length(sequence1) - 1)) {
if (sequence1[i + 1] > sequence1[i]) {
next
} else if (((sequence1[i + 2] > sequence1[i]) & count == 0) & i !=
length(sequence1)-1) {
sequence1 <- sequence1[- (i + 1)]
count <- count + 1
} else if ((sequence1[i + 1] > sequence1[i - 1]) & count == 0 & i != 1) {
sequence1 <- sequence1[-i]
count <- count + 1
} else {
return(FALSE)
}
}
return(TRUE)
}
I've used unlist() because codesignal, for some reason, doesn't accept you to refer to the function argument within the function. This works for some sequences: solution(c(4,1,5)) correctly returns TRUE. It doesn't work for others: solution(c(1, 1, 1, 2, 3)) incorrectly returns TRUE. solution(c(2,1,2,1)) correctly returns FALSE and yet solution(c(1,2,1,2)) incorrectly returns TRUE. I've lost my grip on what's going on. I wonder if anyone can spot anything?
Clarification: the basic idea of my code is to iterate through the sequence and for each element check whether its right neighbour is a bigger number. If it isn't, then we have two options: get rid of i or get rid of i+1, so I check those in turn. Since we can only make one change, i've added the condition that if count is 1, then we skip to finish. Also, if the index is 1 then we can't check i-1, and if the index is length(sequence)-1, then we can't check i+2, so i've added those conditions in to make sure my code skips to the other option if appropriate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个适合我的解决方案。这个想法是,
diff(x)
对于x
中的每个向下步骤都有负元素。例如,如果x
严格递增,则min(diff(x))
为正。如果diff(x)[i] <= 0
对于恰好一个索引i
,我们必须检查是否删除x[i]
或删除x[i+1]
使序列严格递增。以下函数通过了我尝试的所有测试:Here is a solution which works for me. The idea is that
diff(x)
has negative elements for every downwards step inx
. For example,min(diff(x))
is positive, ifx
is strictly increasing. Ifdiff(x)[i] <= 0
for exactly one indexi
, we have to check whether either removingx[i]
or removingx[i+1]
makes the sequence strictly increasing. The following function passed all tests I tried: