Initiating R Practice in HackerRank: Reading stdin and Writing stdout
Having spent the past year-plus traveling and consulting (among other things), I needed a way to stay on top of my coding. Sites like LeetCode and SQLZoo made it pretty easy for me to practice the basics of SQL. To maintain some degree of command of R, I conducted a few projects for fun, some of which I have posted here.
A friend recently introduced me to HackerRank, which enables a similar practice style to LeetCode. HackerRank has many "tracks," which have their own "subdomains." Within in a subdomain, there are problems that you can solve using a variety of languages (e.g. Python, R, C). For instance, the Predicting House Prices problem is in the Artificial Intelligence track, and in the Statistics and Machine Learning subdomain.
This (sum two numbers) was the first problem that I encountered on HackerRank, which was in the Algorithms track, Warmup subdomain. It's meant to introduce you to the HackerRank process of reading and writing data in HackerRank's environment. HackerRank provides the solution for this problem.
# The complete code is given. You can just review and submit!
nums <- read.table("/dev/stdin", sep=" ");
write.table(sum(nums), sep = "", append=T, row.names = F, col.names = F)
Unfortunately, I had a rather frustrating initial experience with HackerRank; I could not use the above reading and writing "template" for the very next problem (sum an array), or any others that I tried. I had difficulty loading HackerRank data, and printing output in HackerRank acceptable format. I could solve this problem in one line in my own environment, i.e., using RStudio on my computer. I didn't find much help in the discussion sections. Stack Exchange provided some help.
To save some time and frustration for those who want to quickly get started in HackerRank to learn R, I prepared the following template based on a compilation of different sources, amending it as needed. The following template is not for people seeking to become professional programmers. Part of learning programming is working through these simple, but important, challenges.
To read the data:
# Open connection called "con" to a file named "stdin"
con <- file("stdin", open = "r")
# Store contents from connection "con" into a variable called "dataset"
dataset <- readLines(con)
For a single line of output, named "out," use special version of print:
cat(out)
For multiple lines of output:
cat(out, sep = "\n")
For example, a complete submission might look something like this:
con <- file("stdin", open = "r")
dataset <- readLines(con)
out <- someFunction(dataset)
cat(out)
close(con)
Reading data properly and getting output in the correct format is important, regardless of your profession. Depending on where you are in your R learning, you may find it useful to figure this out on your own, or jump straight into the practice. If you're in the latter camp, then I wrote this post for you. Happy learning!
Here's a link to my HackerRank profile. In the near future, I plan to post my submissions to my GitHub.