如何检查一个序列是否是“几乎递增序列”?在 R 中?

发布于 2025-01-15 21:42:11 字数 1249 浏览 1 评论 0原文

当我们可以从序列中恰好删除一个元素并得到严格递增序列(即 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2025-01-22 21:42:11

这是一个适合我的解决方案。这个想法是,diff(x) 对于 x 中的每个向下步骤都有负元素。例如,如果 x 严格递增,则 min(diff(x)) 为正。如果 diff(x)[i] <= 0 对于恰好一个索引 i,我们必须检查是否删除 x[i]或删除 x[i+1] 使序列严格递增。以下函数通过了我尝试的所有测试:

check_almost <- function(x) {
  if (length(x) < 2) {
    return(TRUE)
  }
  
  d <- diff(x)
  i <- which(d <= 0)
  if (length(i) == 0) {
    return(TRUE) # strictly increasing
  } else if (length(i) > 1) {
    return(FALSE)
  }

  return(i == 1 || # we can remove x[1]
           i == length(d) ||  # we can remove x[length(x)]
           d[i-1]+d[i] > 0 || # we can remove x[i]
           d[i] + d[i+1] > 0) # we can remove x[i+1]
}

Here is a solution which works for me. The idea is that diff(x) has negative elements for every downwards step in x. For example, min(diff(x)) is positive, if x is strictly increasing. If diff(x)[i] <= 0 for exactly one index i, we have to check whether either removing x[i] or removing x[i+1] makes the sequence strictly increasing. The following function passed all tests I tried:

check_almost <- function(x) {
  if (length(x) < 2) {
    return(TRUE)
  }
  
  d <- diff(x)
  i <- which(d <= 0)
  if (length(i) == 0) {
    return(TRUE) # strictly increasing
  } else if (length(i) > 1) {
    return(FALSE)
  }

  return(i == 1 || # we can remove x[1]
           i == length(d) ||  # we can remove x[length(x)]
           d[i-1]+d[i] > 0 || # we can remove x[i]
           d[i] + d[i+1] > 0) # we can remove x[i+1]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文