Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 1, 2023
1 parent bcabf0f commit 438ca00
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 27 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ ext install ms-python.vscode-pylance

## Generating a template for a task

Open project and run the following command in the VS Code terminal:
Copy the slug of the desired LeetCode problem as shown in the picture:

![plot](./res/slug.png)

Open project and run the following command in the VS Code terminal, specify the previously copied slug:

```bash
python3 gen_task.py "Some Problem"
python3 leetcode.py get concatenation-of-array
```

Open the following files:
The command above will generate several files, open them in the terminal:

```bash
code solutions/some-problem/solution.hpp
code solutions/some-problem/test.cpp
code solutions/concatenation-of-array/solution.hpp
code solutions/concatenation-of-array/test.cpp
```

After writing the solution to the problem and the tests, use the VS Code status bar to run the tests:
Expand All @@ -47,6 +51,12 @@ After writing the solution to the problem and the tests, use the VS Code status
Or use TestMate extension:
![plot](./res/test-mate.png)

The command to get the LeetCode Daily question:

```bash
python3 leetcode.py get-daily
```

## Auto-Format in VS Code

Install clang-format in Ubuntu:
Expand Down
108 changes: 86 additions & 22 deletions gen_task.py → leetcode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from argparse import ArgumentParser
from argparse import ArgumentParser, BooleanOptionalAction
from dataclasses import dataclass
from glob import glob
from pathlib import Path

import requests
Expand Down Expand Up @@ -40,6 +41,8 @@
"Hard": "red",
}

LEETCODE_GRAPHQL_URL = "https://leetcode.com/graphql"


@dataclass
class Task:
Expand All @@ -53,9 +56,29 @@ class Task:
meta_data: str


def get_task_info(task_slug: str) -> Task:
url = "https://leetcode.com/graphql"
def get_daily_question_slug() -> Task:
query = """
query questionOfToday {
activeDailyCodingChallengeQuestion {
question {
titleSlug
}
}
}
"""

response = requests.post(
LEETCODE_GRAPHQL_URL,
json={
"query": query,
},
)
data = response.json()["data"]
question = data["activeDailyCodingChallengeQuestion"]["question"]
return question["titleSlug"]


def get_task_info(task_slug: str) -> Task:
query = """
query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
Expand All @@ -80,7 +103,7 @@ def get_task_info(task_slug: str) -> Task:
"query": query,
}

response = requests.post(url, json=data)
response = requests.post(LEETCODE_GRAPHQL_URL, json=data)
question = response.json()["data"]["question"]

snippets = {s["langSlug"]: s["code"] for s in question["codeSnippets"]}
Expand Down Expand Up @@ -116,10 +139,28 @@ def update_cmake(task_slug: str) -> None:
file.write(task)


def create_problem_description_file(task_path: Path, task: Task) -> None:
with open(task_path / DESCRIPTION_FILE, "w") as file:
file.write(
DESCRIPTION_FILE_CONTENT.format(
frontend_id=task.frontend_id,
title=task.title,
title_slug=task.title_slug,
color=DIFFICULTY_COLOR_MAP[task.difficulty],
difficulty=task.difficulty,
content=task.content,
)
)


def create_task_folder(task_slug: str) -> None:
task = get_task_info(task_slug)

task_path = Path("solutions") / Path(task_slug)

if Path.exists(task_path):
print(f"Already exists: {task_path}")
return

task_path.mkdir()

with open(task_path / SOLUTION_FILE, "w") as file:
Expand All @@ -139,34 +180,57 @@ def create_task_folder(task_slug: str) -> None:
)
)

with open(task_path / DESCRIPTION_FILE, "w") as file:
file.write(
DESCRIPTION_FILE_CONTENT.format(
frontend_id=task.frontend_id,
title=task.title,
title_slug=task.title_slug,
color=DIFFICULTY_COLOR_MAP[task.difficulty],
difficulty=task.difficulty,
content=task.content,
)
)
create_problem_description_file(task_path, task)

update_cmake(task_slug)

print(f"Created file: {task_path / SOLUTION_FILE}")


if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"--slug",
subparsers = parser.add_subparsers(title="Commands", dest="cmd")

get_task = subparsers.add_parser(
"get",
help="Get LeetCode Problem",
)
get_task.add_argument(
"slug",
type=str,
required=True,
help="LeetCode Problem Slug",
)
parser.add_argument(
get_task.add_argument(
"--lang",
type=str,
choices=["cpp"],
default="cpp",
required=False,
)

get_daily = subparsers.add_parser(
"get-daily",
help="Get Daily LeetCode Problem",
)

get_descriptions = subparsers.add_parser(
"get-descriptions",
help="Update LeetCode Problems Description",
)

args = parser.parse_args()

create_task_folder(args.slug)
# update_cmake(args.slug)
if args.cmd is None:
parser.print_help()
elif args.cmd == "get":
create_task_folder(args.slug)
elif args.cmd == "get-daily":
create_task_folder(get_daily_question_slug())
elif args.cmd == "get-descriptions":
for path in glob("./solutions/*", recursive=False):
task_path = Path(path)
task = get_task_info(task_path.name)
create_problem_description_file(task_path, task)
else:
print(f"Unknown command {args.cmd}")
parser.print_help()
Binary file added res/slug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/status-bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res/test-mate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 438ca00

Please sign in to comment.