Generate test cases for your problem easily. Credits to @aashutoshrathi.
HackGen is on PyPI, so you can use pip to install it:
# bash
$ pip install hackgen
After installation, you can get started!
For the example lets take the simple problem "Clock Delay" as the problem you have created.
- 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.
- 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
This can be python, java, c++, c file. If you need support other languages please update the Language
class in the language.py file.
- Your solution logic.py file.
q = int(input())
for i in range(q):
h1, m1, h2, m2 = map(int, input().split())
k = int(input())
delay = (h1 + k - h2) * 60 + m1 - m2
print(delay)
- Your solution Logic.java file.
import java.util.Scanner;
public class Logic {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int q = scanner.nextInt();
for (int i = 0; i < q; i++) {
int h1, m1, h2, m2, k;
h1 = scanner.nextInt();
m1 = scanner.nextInt();
h2 = scanner.nextInt();
m2 = scanner.nextInt();
k = scanner.nextInt();
int delay = (h1 + k - h2) * 60 + m1 - m2;
System.out.println(delay);
}
}
}
Create a file clockdelay.clock_delay.py in the same directory the logic.py file contains.
Create the class ClockDelayInputFormat
extending hackgen.TestInputFormat
and overriding inputs(difficult_level: int) -> None
method with constraints and input format identified in the second step.
You can introduce difficulty with using difficult_level
value which is between 0 and 9 inclusively [0-9].
import random
from hackgen import TestInputFormat, TestGenerator, Language
class ClockDelayInputFormat(TestInputFormat):
"""
Input format of Clock Delay challenge.
https://www.hackerrank.com/contests/hourrank-28/challenges/clock-delay
"""
# difficulty levels with test file number
# difficulty level is [0-9]
__diff = [(5, 10), (10, 30), (50, 100), (100, 300), (100, 300),
(300, 600), (600, 900), (800, 1000), (900, 1000), (950, 1000)]
def inputs(self, difficult_level: int) -> None:
q = random.randint(*self.__diff[difficult_level]) # number of test cases
print(q)
for n in range(q):
# constraints for h1 m1 h2 m2 k
h1 = random.randint(0, 23)
m1 = random.randint(0, 60)
h2 = random.randint(h1, 24)
k = random.randint(h2 - h1 + 1 if h1 == h2 else h2 - h1, 24 - h1)
m2 = random.randint(0, (m1 if h1 + k == h2 else 60))
print(h1, m1, h2, m2)
print(k)
# input format instance
input_format = ClockDelayInputFormat()
# try with Language.java('Logic') also
test_generator = TestGenerator(10, input_format, Language.python('logic'), "ClockDelay")
test_generator.run()
Create instance inputFormat
of clockdelay.ClockDelayInputFormat
class. Create generator using hackgen.TestGenerator
class with required information.
- 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
Execute the script clock_delay.py.
Run the following in a terminal.
$ cd examples/clockdelay/ && python clock_delay.py
See the file ${Name}-test-cases.zip
contains in the directory examples/clockdelay/
.
HackerRank Test Case Generator is MIT licensed.
Contributions are welcome! :)