Git Product home page Git Product logo

leetcode_unit's Introduction

leetcode_unit

leetcode_unit is a lightweight and offline tool to generate solutions and test cases for the problems in Leetcode. leetcode_unit has the following features:

  1. Automatic generate template files for a new leetcode problem
  2. Add customized unit tests for each problem
  3. Compile and run any solution with one command.
  4. Debug the code with gdb with one command.

Prerequisites

It has been tested on Ubuntu 20.04 and Windows Subsystem for Linux (WSL) v2.

leetcode_unit has been tested based on following versions:

  1. python3 (>=3.8.10)
  2. make (>=4.2.1) and g++(>=8.3.0)
  3. cppunit (>=1.14.0)
  4. gdb (>=9.2)

you can install them using apt

sudo apt install make g++ libcppunit-dev

On WSL v2, you also must install Ubuntu 20.04 version. Besides the above tools and libraries, it is better to install vscode.

Ideally, leetcode_unit can be used at other Linux platforms.

Download leetcode_unit

git clone https://github.com/OctHe/leetcode_unit
cd leetcode_unit

To begin with leetcode_unit, please read the usage with ./leetcode_unit --help or ./leetcode_unit -h

Add New Problem

The following content uses the Problem 1 as an example.

The program is a python program named as leetcode_unit.py. To add a new problem, you can directly run the following command

./leetcode_unit add -i 1 -n Two_Sum

where -i is the index of the problem and -n is its name.

After running the command, a new directory will be created in the ./problems/ directory like this:

.   
├── problems    
│   ├── 1_Two_Sum     
│   │   ├── 1_Two_Sum.h   
│   │   └── Test_Cases_1.cc    

Until now, you can add your own solution to this new problem.

Add Solution

1_Two_Sum.h contains Solution class. Open it with a editor, you need to replace the following contents:

<return_type> <function>
{

}

by your solution, such as:

vector<int> twoSum(vector<int>& nums, int target) 
{
    vector<int> sum;
    int result;
    for(int i = 0; i < nums.size(); i++)
    {
        result = target - nums[i];
        for(int j = i+1; j < nums.size(); j++)
        {
            if(result == nums[j])
            {
                sum.push_back(i);
                sum.push_back(j);
            }
        }
    }
    return sum;
}

Run Solution

leetcode_unit provides a command to run the solution.

./leetcode_unit.py run -i 1

This command will automatically compile (for C and C++) and run the program of Problem 1.

If the program has no error, you can see the output

OK (1 tests)

Note that the results will be always OK because test cases have not been added.

Add Test Cases

Test cases for Problem 1 can be saved in file Test_Cases_1.cc like this

void test_problem_1::test_case_1()
{
    // A simple assert: CPPUNIT_ASSERT
}

The following code can be added in the function

void test_problem_1::test_case_1()
{
    vector<int> input = {2, 7, 11, 15};
    int target = 9;
    
    vector<int> output = {0, 1};

    CPPUNIT_ASSERT(s->twoSum(input, target) == output);
}

Compile and run it with

./leetcode_unit.py run -i 1

TODO List

  1. ``open'' sub-command (work with gvim)
  2. Time and space complexity of each problem
  3. python style (Google python style)
  4. problem info in template
  5. Helper data structures for unit tests of list and binary tree

leetcode_unit's People

Contributors

octhe avatar

Watchers

 avatar

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.