hackgen — Generate Test cases for HackerRank Problems

Open-source test case generator for your creative hackrank problem

Renuka Fernando
2 min readNov 7, 2018
image src: https://i0.wp.com/gradsingames.com/wp-content/uploads/2016/05/856771_668224053197841_1943699009_o.png

Writing a solution for a HackerRank problem may be an easy task compared to creating a HackerRank problem. Actually, it is not an easy task. However, manually creating “test cases” for the problem may add additional overhead. If you want to generate test cases automatically one of the best solutions is “hackgen”.

GitHub Repository: https://github.com/renuka-fernando/hackgen

Hackgen is an open-source tool that will generate test cases for the solution algorithm you provide with 10 difficulty levels. I am going to go through a simple problem in HackerRank and assume this is the question that you have created.

First of all, you have to install hackgen via pypi (https://pypi.org/project/hackgen/).

$ pip install hackgen

The problem (assume this is the problem you created): Clock Delay

Let’s study the input format and the constraints of the problem.

Input Format

  • The first line contains q, the number of queries.
  • Each query is described by two lines. The first line contains four space-separated integers h1, m1, h2, m2. The second line contains a single integer k.

Constraints

  • 1 ≤ q ≤ 1000
  • 0 ≤ h1 < 23
  • 0 ≤ h2 < 24
  • 0 ≤ m1, m2 < 60
  • 1 ≤ k
  • h1 + k < 24
  • It is guaranteed that h1:m1 is strictly before h2:m2

The following is my solution for the challenge using python (You should have your solution for your problem 😊).

1. Define Input Format

Your problem has an input format. So let’s inform “hackgen” about this. Let’s create a class called ClockDelayInputFormat which extends hackgen’s “TestInputFormat” class with overriding its method inputs.

You want difficult levels. hackgen supports 10 levels of difficulties. For the easiness let’s take the number of queries (variable q) as the difficulty. You can configure this as not only for the number of queries but also for input value range.

print(q)
print(h1, m1, h2, m2)
print(k)

Above code lines in the ClockDelayInputFormat class actually the input format of the problem.

2. Generate Test Cases

Get an instance of the ClockDelayInputFormat class: input_format.

Suppose you want 10 test files.

  • Number of Test Files Needs: 10
  • Instance of TestInputFormat: ClockDelayInputFormat
  • Language of Solution File (python, java, c++, c) and File Name: python(logic) also try with java(Logic)
  • Name of the Problem: ClockDelay

Define those values and execute the hackgen’s run method to generate your test files.

Execute the script clock_delay.py by Running the following in a terminal.

$ cd examples/clockdelay/ && python clock_delay.py

3. Find Your Test Files

Congratulations!!! 🥂 Now you have required test cases with you. Browse the file ${Name}-test-cases.zip contains in the directory examples/clockdelay/. Upload the zip file and enjoy!!!

References

  1. https://github.com/renuka-fernando/hackgen
  2. https://pypi.org/project/hackgen/

--

--