HackerRank in Swift - Makefiles
A basic makefile to help you with testing your HackerRank solutions locally.
So you are into HackerRank and want to be able to run the code locally before submitting it. This is useful when you have one of the test cases failing and you want to debug it and find the problem.
In this article I’ll show how you can run Haskell and Swift code locally via command line in a convenient way.
Before you automate any process, you need to understand what’s there to automate in the first place. The usual approach is to run all the commands manually, so you can notice common patterns and find out certain edge-cases.
Let’s look at first challenge, which is already solved in this article. Let’s agree that all test cases will be put in
tc folder, then create
tc/solve-me-first-tc0.txt with the following contents.
tc<N> part of the file name stands for “test case N”, the default test case provided as part of assignment has index
0. So to run your code against test cases 0 and 1, you would run commands like these.
Clearly there’s a pattern there, let’s express it using bash syntax.
The script above is a valid bash script. With little work you can turn it into a script that takes file name as a argument and does all the magic of figuring out the extension and interpreter command under the hood, but there is a better way (in my humble opinion).
Yes, makefiles. I already wrote about them in regards to automating mobile CI tasks. Check that write up if you need to refresh some make and makefile basics. So let’s turn bash script from previous paragraph into a makefile.
Nice feature or makefiles is pattern matching for targets based on the input. If you run this makefile specifying Haskell source file as input (that means file has
.hs extension), it will match agains
%.hs target and run commands for Haskell.
patsubst is short for “path substitution” and turns input file name into a test case file name, here
$@ is the way to refer to target name.
Save it as, say,
Makefile and give it a try.
Works as expected. All you have to do now for each new assignment is to create source file and test case files and then run
make. Annoying part is that you have to specify makefile name explicitly each time. Say, you have
hackerrank directory with
Makefile in it, and you organize assignments in their own directories.
So if your working directory is
warmup and you want to run assignment code from there, you have to do somethign like this
The solution is to use an alias like this
Put this alias in your shell profile and now running assignments code from any folder is as easy as
Just like with shell scripts, there a bits of reusable code noticeable in the makefile. With use of functions and bit of refactoring, it can be turned into this.
This can be improved and improved until the moment your internal perfectionist is happy, but let’s stop here for now. More important feature of this makefile is how easy it is to add another interpretable language support, for example Ruby.
Use slightly modified Ruby solution for Solve me first from HackerRank and try it out.
blog comments powered by Disqus