Skip to content

Instantly share code, notes, and snippets.

@allenaven
Created July 7, 2017 06:52
Show Gist options
  • Save allenaven/37fdb05c8e697c6a76f4317ea7577554 to your computer and use it in GitHub Desktop.
Save allenaven/37fdb05c8e697c6a76f4317ea7577554 to your computer and use it in GitHub Desktop.
Hackerrank templates demonstrating how to appropriately handle the Stdin and Stdout in their code exercises
# There are a few ways in R to read Stdin. The one that works
# with Hackerrank problems is to open it as a file--
f <- file("stdin")
on.exit(close(f))
# Calling readLines(f) returns a vector of strings, one element
# per line of input. You'll have to explicitely convert to int
# if you're expecting an integer on that line,
# or possibly use readr::parse_integer() ?
in_strings <- readLines(f)
# Now loop over the lines of input and write each back to Stdout
for(i in 1:length(in_strings)) {
the_str <- in_strings[i]
write(the_str, stdout())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment