Skip to main content

Fork of Leetscrape with resolved pandas issues and bumped pydantic

Project description

LeetScrape

deploy-docs PYPI codecovPyPI - Downloads

Introducing the LeetScrape - a powerful and efficient Python package designed to scrape problem statements and basic test cases from LeetCode.com. With this package, you can easily download and save LeetCode problems to your local machine, making it convenient for offline practice and studying. It is perfect for software engineers and students preparing for coding interviews. The package is lightweight, easy to use and can be integrated with other tools and IDEs. With the LeetScrape, you can boost your coding skills and improve your chances of landing your dream job.

Use this package to get the list of Leetcode questions, their topic and company tags, difficulty, question body (including test cases, constraints, hints), and code stubs in any of the available programming languages.

Detailed documentation available here.

There is also a related Next.js web app to serve the scraped questions and your answers at leetcode-nextjs. See the demo.

Get Started

Installation

Start by installing the package from pip or conda:

# Using pip
pip install leetscrape

# Using conda:
conda install leetscrape

# Using poetry:
poetry add leetscrape

Usage

Command Line

  • leetscrape list [--out OUT] - List all questions without generating code stub.

    options:
    -h, --help         show a help message and exit
    --out OUT, -o OUT  Specify the output file name to store the list of questions.
    
  • leetscrape question [--out OUT] qid [qid ...] - Generate a code stub for the given question(s).

    positional arguments:
    qid                Enter Leetcode question ID(s)
    
    options:
    -h, --help         show this help message and exit
    --out OUT, -o OUT  Enter the path to the output directory
    
  • leetscrape solution [-h] [--out OUT] input - Generate mdx files from solutions.

    positional arguments:
    input              Enter the path to the solution directory with solution files or to a single
                        solution file
    
    options:
    -h, --help         show this help message and exit
    --out OUT, -o OUT  Enter the path to the output directory to save solutions mdx files
    
  • leetscrape ts [--out OUT] - Create the leetscrape-ts Next.js project to host the solutions.

    options:
    -h, --help         show this help message and exit
    --out OUT, -o OUT  Enter the path to the output directory to save the project
    

Python API

Get the list of problems and their information
from leetscrape_updated import GetQuestionsList

ls = GetQuestionsList()
ls.scrape() # Scrape the list of questions
ls.questions.head() # Get the list of questions
QID title titleSlug difficulty acceptanceRate paidOnly topicTags categorySlug
0 1 Two Sum two-sum Easy 51.4225 False array,hash-table algorithms
1 2 Add Two Numbers add-two-numbers Medium 41.9051 False linked-list,math,recursion algorithms
2 3 Longest Substring Without Repeating Characters longest-substring-without-repeating-characters Medium 34.3169 False hash-table,string,sliding-window algorithms
3 4 Median of Two Sorted Arrays median-of-two-sorted-arrays Hard 38.8566 False array,binary-search,divide-and-conquer algorithms
4 5 Longest Palindromic Substring longest-palindromic-substring Medium 33.4383 False string,dynamic-programming algorithms

You can export the associated tables to a directory using the to_csv method:

ls.to_csv(directory="<dir>")

This generates 6 .csv files in the current directory:

  • questions.csv - List of questions with their title, difficulty, acceptance rate, paid status, topic tags, and category.
  • companies.csv - List of companies with their name, slug, and the questions count.
  • topicsTags.csv - List of topic tags with their name and slug.
  • categories.csv - List of categories with their name and slug.
  • questionCategory.csv - An edgelist of questions and their categories.
  • questionTopics.csv - An edgelist of questions and their topic tags.
Get Question statement and other information

Query individual question's information such as the body, test cases, constraints, hints, code stubs, and company tags using the GetQuestion class:

from leetscrape_updated import GetQuestion

# Get the question body
question = GetQuestion(titleSlug="two-sum").scrape()

This returns a Question object with the following attributes:

question.QID # Question ID
question.title # Question title
question.titleSlug # Question title slug
question.difficulty # Question difficulty
question.Hints # Question hints
question.Companies # Question companies
question.topics # Question topic tags
question.SimilarQuestions # Similar questions ids
question.Code # Code stubs
question.Body # Question body / problem statement
question.isPaidOnly # Whether the question is only available to premium users of Leetcode
Generate code stubs for a question
from leetscrape_updated import GenerateCodeStub

# Get the question body
fcs = GenerateCodeStub(titleSlug="two-sum")
fcs.generate(directory="<dir>")

This generates the following files in the given directory:

  • q_0001_twoSum.py - Python file with the code stub for the given question with a function named twoSum.
  • test_q_0001_twoSum.py - Python file with the test cases for the given question.

See examples for examples of the generated code stubs.

Generate mdx files from solutions

Once you have solved a question, you can generate an mdx file with the solution and the question statement using the ExtractSolutions class:

from leetscrape_updated import ExtractSolutions

# Get the question body
solutions = ExtractSolutions(filename="<path-to-solution-file>").extract()

This outputs a list of Solution objects with the following attributes:

solution.id # Solution ID
solution.code # Solution code
solution.docs # Docstrings associated with the solution
solution.problem_statement # Question body / problem statement

Alternatively, you can use the to_mdx method to generate the mdx file:

from leetscrape_updated import ExtractSolutions

# Get the question body
ExtractSolutions(filename="<path-to-solution-file>").to_mdx(output_filename="<path-to-output-file>")

Serving the solutions with leetscrape-ts

You can use the leetscrape-ts Next.js template to serve the solutions. See the demo. Visit the repo for more details. You can generate the project using the leetscrape ts command:

leetscrape ts --out <path-to-output-directory>

This will bootstrap the project in the given directory. Follow the instructions in the README and create/modify the .env.local file. Then, run the following command to generate the mdx files:

leetscrape solution --out <path-to-output-directory>/src/content/solutions <path-to-your-python-solution-directory>

You can then run the project using the following command:

cd <path-to-output-directory>
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev

Changelog

View the changelog here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

leetscrape_updated-0.0.1.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

leetscrape_updated-0.0.1-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file leetscrape_updated-0.0.1.tar.gz.

File metadata

  • Download URL: leetscrape_updated-0.0.1.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.4

File hashes

Hashes for leetscrape_updated-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0b2b1d50cf6eeec2a2b95290e9e3423b66e6e18b3007c3bb2d18723680e36374
MD5 5cad7327800208ea72ec445f7f27073b
BLAKE2b-256 fee90cc0ecba1730e3cf18c6bf396041aef36f079c11d19f507de2e2c89ca394

See more details on using hashes here.

File details

Details for the file leetscrape_updated-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for leetscrape_updated-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 23d53cbb551651ff64fdd1dab152406c8a04ace467c7414cd53a44d714d16b3e
MD5 705a2711a77f23e623e835b676b4be94
BLAKE2b-256 9b0066c62afd5fcc677af95eebe95e277f4de7a6df6633bd295d57103cfb5d64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page