Coder Social home page Coder Social logo

hackgen's Introduction

HackGen: HackerRank Test Case Generator

Build Status HitCount

Generate test cases for your problem easily. Credits to @aashutoshrathi.

Table of Contents

1. Installation in Python

HackGen is on PyPI, so you can use pip to install it:

# bash
$ pip install hackgen

After installation, you can get started!

2. Getting Started

For the example lets take the simple problem "Clock Delay" as the problem you have created.

2.1. Identify your Input Format and Constraints

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

2.2. Your Solution Algorithm

This can be python, java, c++, c file. If you need support other languages please update the Language class in the language.py file.

  1. 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)
  1. 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);
        }
    }
}

2.3. Define Input Format

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.

2.3.1. Required Information for TestGenerator

  • 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

2.4. Find the Zipped Test Files

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/.

3. License

HackerRank Test Case Generator is MIT licensed.

4. Contributions

Contributions are welcome! :)

hackgen's People

Contributors

fostiropoulos avatar leandrovianna avatar renuka-fernando avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hackgen's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.