You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Hackerrank生成R语言输出?FizzBuzz代码报错求助

Hey there! Let's tackle your two HackerRank R questions one by one.

1. How to Create R Output for HackerRank

HackerRank expects your R code to output results directly to standard output (stdout) without extra formatting fluff (like the [1] prefix that print() adds). Here's what you need to know:

  • Use cat() instead of print(): cat() writes directly to stdout and lets you control formatting exactly.
  • For multi-line output (like most problems require), use sep = "\n" to put each result on its own line.
  • Example: If you need to output numbers 1 to 5, just run cat(1:5, sep = "\n")—this will print each number on a separate line, which is exactly what HackerRank looks for.
2. Fixing Your FizzBuzz Code for HackerRank

Let's walk through the issues in your code and fix them step by step:

Key Problems in Your Original Code

  1. Redundant stdin2 initialization: You don't need to predefine stdin2 with c(NA, n)—this creates a vector of the wrong length and leads to unnecessary complexity.
  2. Incorrect write() usage: cat() already outputs directly to stdout, and its return value is NULL. Calling write(cat(...), stdout()) is trying to write NULL to stdout, which is why you're getting errors. This line is completely unnecessary.
  3. Formatting issue: cat(stdin2, "\n") will print all elements in one line followed by a single newline. HackerRank expects each result on its own line, so you need sep = "\n" instead.
  4. Minor logical tweak: In your nested ifelse, you return i (the loop index) instead of set[i]—it works here because set is 1 to n, but it's better to use the actual number for clarity and robustness.

Corrected Code

fizzBuzz <- function(n) { 
  set <- seq.int(1, n, 1) 
  # Initialize a character vector to store results (consistent type)
  result <- character(length(set)) 
  
  for (i in seq_along(set)){ 
    num <- set[i]
    if (num %% 15 == 0) {
      result[i] <- "FizzBuzz"
    } else if (num %% 3 == 0) {
      result[i] <- "Fizz"
    } else if (num %% 5 == 0) {
      result[i] <- "Buzz"
    } else {
      # Convert number to character to keep vector type consistent
      result[i] <- as.character(num) 
    }
  } 
  # Print each result on a separate line
  cat(result, sep = "\n") 
}

What Changed?

  • Replaced nested ifelse with plain if-else blocks for better readability.
  • Used seq_along(set) instead of 1:length(set)—this is safer because it handles empty vectors correctly.
  • Initialized result as a character vector to avoid mixing numeric and character values (which can cause unexpected behavior).
  • Removed the broken write() call and used cat(..., sep = "\n") to output each result on its own line, matching HackerRank's requirements.

内容的提问来源于stack exchange,提问作者eknagi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 23:07:40