Skip to content

Latest commit

 

History

History
188 lines (162 loc) · 5.88 KB

File metadata and controls

188 lines (162 loc) · 5.88 KB

OpenLeetCode - An open source version of LeetCode

Welcome to the OpenLeetCode Project!

The motivation behind this project is to be able to practice LeetCode problems on a plane without requiring an internet connection (until Starlink ramps up). This project is not intended to replace or replicate leetcode.com.

Table of Content

Screenshot

Screenshot

Build

Windows

Building without UI

git clone https://github.com/mbucko/openleetcode
cd openleetcode
.\install --prefix=./install

Building with UI

git clone https://github.com/mbucko/openleetcode
cd openleetcode
.\install --prefix=./install --enable_ui

Unix

Building without UI

git clone https://github.com/mbucko/openleetcode
cd openleetcode
./install.sh --prefix=./install

Building with UI

git clone https://github.com/mbucko/openleetcode
cd openleetcode
./install.sh --prefix=./install --enable_ui

Run

Windows

CLI

dir install/OpenLeetCode
./openleetcode --language cpp --problem TwoSum

UI

dir install/OpenLeetCode
./openleetcodeui

Unix

CLI

cd install/OpenLeetCode
./openleetcode.sh --language cpp --problem TwoSum

Python is also supported in the CLI, but C++ remains the default documented path because the project is fully implemented end to end around it:

cd install/OpenLeetCode
uv sync   # or: pip install jsonschema
./openleetcode.sh --language python --problem TwoSum

UI

cd install/OpenLeetCode
./openleetcodeui.sh

NOTE: UI for unix is yet to be tested.

How To Use

After the build succeeds, the following directory structure will be generated:

  • problems
    • NumberOfIslands
      • cpp
        • solution.cpp
        • ...
      • testcases
        • TestCase1.test
        • TestCase2.test
        • ...
      • description.md
    • TwoSum
      • ..
  • launguage
    • cpp
    • python

Just like for LeetCode, you have one file where you solve the problem. The default workflow uses problems/TwoSum/cpp/solution.cpp. A CLI-only Python path is also available at problems/TwoSum/python/solution.py. To add new test cases, you can create a file in the problems/TwoSum/testcases/ directory with the file extension .test, and the solution will automatically be tested against it.

Each problem is described in the description.md file location in the problem's directory. For example problems/TwoSum/description.md.

The format of the .test files are as follows:

<arg1>
<arg2>
<expected results>

The supported types are: integral type, a string, TreeNode structure, boolean or an array. For example:

["1", "2", "4"]
8.0
[0, 0]

A ThreeNode structure is represented in an array-based structure. For example an array representation [1, 2, null, null, 3] results to the following structure:

  1
 / \
2   3

List of LeetCode Problems

  • TwoSum
  • LongestSubstringWithoutRepeatingCharacters
  • NumberOfIslands
  • CheckCompletenessOfABinaryTree

The problem names are automatically extracted from the problems folder.

Usage

$ python openleetcode.py --help
usage: openleetcode.py [-h] [--language {cpp,python}] [--list-problems] [--list-testcases] [--problem problem_name] [--problem_builds_dir dir] [--testcase testcase_name] [--verbose]

OpenLeetCode problem builder. This script builds and tests LeetCode-like problems locally. It currently supports the C++ and Python languages, but it can be extended to support other languages.

options:
  -h, --help            show this help message and exit
  --language {cpp,python}, -l {cpp,python}
                        The programming language.
  --list-problems       List problems.
  --list-testcases      List testcases for a problem specified with '--problem' option.
  --problem problem_name, -p problem_name
                        Name of the problem to build and test. Default: TwoSum. Use --list-problems to list all problems.
  --problem_builds_dir dir, -d dir
                        Specifies the directory with the problems. Typically, this is './problem_builds'. If not provided, the script defaults to './problem_builds' in the same directory as the executable.
  --run-expected-tests, -r
                        Run the expected solution. Default: False.
  --testcase testcase_name, -t testcase_name
                        Name of the testcase to run. '--testcase All' will run all testcases. Default: All.
  --verbose, -v         Print verbose output

Note

The CLI currently supports C++ and Python. C++ is still the primary end-to-end workflow, and the Electron UI is still C++-only.

Requirements

This project requires the following to run:

  • Python
  • CMake 3.12
  • Git

Additional Requirements for the UI

  • npm

Contributing

Feel free to contribute with code, test cases, or even code reviews.

For a more in-depth guide on how to contribute and information about the inner workings of OpenLeetCode, please refer to the Docs. You can also join our Discord chat if you have any questions about the usage or development.