feat: create template with problem url and code snippet
This commit is contained in:
parent
8a95ea80a7
commit
ec5559f877
3 changed files with 70 additions and 9 deletions
5
Makefile
5
Makefile
|
|
@ -1,4 +1,7 @@
|
||||||
lint:
|
lint:
|
||||||
golangci-lint run ./...
|
golangci-lint run ./...
|
||||||
|
|
||||||
.PHONY: lint
|
problemset:
|
||||||
|
bash ./query-problem-set.sh > problemset.jsonl
|
||||||
|
|
||||||
|
.PHONY: lint problemset
|
||||||
|
|
|
||||||
37
create.sh
37
create.sh
|
|
@ -1,21 +1,42 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
num=$1
|
id=$1
|
||||||
|
|
||||||
echo "$num" | grep -q "^[1-9][0-9]\{,4\}$" || {
|
echo "$id" | grep -q "^[1-9][0-9]\{,4\}$" || {
|
||||||
echo "Err: no valid number given" >&2
|
echo "Err: no valid problem ID provided." >&2
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
parent=$((num / 100))
|
parent=$((id / 100))
|
||||||
pdir="solutions/${parent}/q${num}"
|
pdir="solutions/${parent}/q${id}"
|
||||||
sol_file="${pdir}/solution.go"
|
sol_file="${pdir}/solution.go"
|
||||||
|
|
||||||
if [ -f "$sol_file" ]; then
|
if [ -f "$sol_file" ]; then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Creating template for question No. $num" >&2
|
title_slug=$(jq -r "select(.id == $id) | .titleSlug" ./problemset.jsonl)
|
||||||
|
[ -n "$title_slug" ] || {
|
||||||
|
echo "Err: problem $id not found in the problem set." >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
problem_url="https://leetcode.com/problems/$title_slug/"
|
||||||
|
|
||||||
|
echo -e "Creating template for problem \033[32m${id}: $title_slug\033[0m" >&2
|
||||||
mkdir -pv "$pdir"
|
mkdir -pv "$pdir"
|
||||||
echo "package q$num" > "$sol_file"
|
echo "// Package q$id implements a solution for $problem_url" > "$sol_file"
|
||||||
echo "Created $sol_file"
|
echo -e "package q$id\n" >> "$sol_file"
|
||||||
|
|
||||||
|
echo "Created: $sol_file"
|
||||||
|
|
||||||
|
echo "Fetching code snippet for Go ..."
|
||||||
|
curl -sSLf "${problem_url}description/" \
|
||||||
|
-H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8' \
|
||||||
|
-H 'accept-language: en-US,en;q=0.5' \
|
||||||
|
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36' \
|
||||||
|
| grep -o 'NEXT_DATA[^>]*>.*' \
|
||||||
|
| sed 's/^[^>]*>//;s/<\/script.*//' \
|
||||||
|
| jq -r '[ .props.pageProps.dehydratedState.queries[].state.data.question.codeSnippets | select( . != null) | .[] | select(.langSlug == "golang") ][0].code' \
|
||||||
|
>> "$sol_file"
|
||||||
|
|
||||||
|
echo "Done."
|
||||||
|
|
|
||||||
37
query-problem-set.sh
Normal file
37
query-problem-set.sh
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
query() {
|
||||||
|
page=$1
|
||||||
|
offset=$((page * 100))
|
||||||
|
|
||||||
|
# shellcheck disable=SC2016
|
||||||
|
curl -sSLf 'https://leetcode.com/graphql/' \
|
||||||
|
--compressed \
|
||||||
|
-H 'content-type: application/json' \
|
||||||
|
--data-raw '{
|
||||||
|
"query": "\n query problemsetQuestionListV2($filters: QuestionFilterInput, $limit: Int, $searchKeyword: String, $skip: Int, $sortBy: QuestionSortByInput, $categorySlug: String) {\n problemsetQuestionListV2(\n filters: $filters\n limit: $limit\n searchKeyword: $searchKeyword\n skip: $skip\n sortBy: $sortBy\n categorySlug: $categorySlug\n ) {\n questions {\n id\n titleSlug\n title\n translatedTitle\n questionFrontendId\n paidOnly\n difficulty\n topicTags {\n name\n slug\n nameTranslated\n }\n status\n isInMyFavorites\n frequency\n acRate\n contestPoint\n }\n totalLength\n finishedLength\n hasMore\n }\n}\n ",
|
||||||
|
"variables": {
|
||||||
|
"skip": '"$offset"',
|
||||||
|
"limit": 100,
|
||||||
|
"categorySlug": "all-code-essentials",
|
||||||
|
"filters": {"filterCombineType":"ALL","statusFilter":{"questionStatuses":[],"operator":"IS"},"difficultyFilter":{"difficulties":[],"operator":"IS"},"languageFilter":{"languageSlugs":[],"operator":"IS"},"topicFilter":{"topicSlugs":[],"operator":"IS"},"acceptanceFilter":{},"frequencyFilter":{},"frontendIdFilter":{},"lastSubmittedFilter":{},"publishedFilter":{},"companyFilter":{"companySlugs":[],"operator":"IS"},"positionFilter":{"positionSlugs":[],"operator":"IS"},"positionLevelFilter":{"positionLevelSlugs":[],"operator":"IS"},"contestPointFilter":{"contestPoints":[],"operator":"IS"},"premiumFilter":{"premiumStatus":[],"operator":"IS"}},
|
||||||
|
"searchKeyword": "",
|
||||||
|
"sortBy": {"sortField":"CUSTOM","sortOrder":"ASCENDING"},
|
||||||
|
"filtersV2": {"filterCombineType":"ALL","statusFilter":{"questionStatuses":[],"operator":"IS"},"difficultyFilter":{"difficulties":[],"operator":"IS"},"languageFilter":{"languageSlugs":[],"operator":"IS"},"topicFilter":{"topicSlugs":[],"operator":"IS"},"acceptanceFilter":{},"frequencyFilter":{},"frontendIdFilter":{},"lastSubmittedFilter":{},"publishedFilter":{},"companyFilter":{"companySlugs":[],"operator":"IS"},"positionFilter":{"positionSlugs":[],"operator":"IS"},"positionLevelFilter":{"positionLevelSlugs":[],"operator":"IS"},"contestPointFilter":{"contestPoints":[],"operator":"IS"},"premiumFilter":{"premiumStatus":[],"operator":"IS"}}
|
||||||
|
},
|
||||||
|
"operationName": "problemsetQuestionListV2"
|
||||||
|
}' | jq -c '.data.problemsetQuestionListV2.questions[] | { id, title, titleSlug, difficulty, paidOnly, tags: [ .topicTags[] | .slug ] }'
|
||||||
|
}
|
||||||
|
|
||||||
|
page=0
|
||||||
|
while true; do
|
||||||
|
echo -ne " Querying page $((page + 1)) ...\r" >&2
|
||||||
|
content=$(query $page)
|
||||||
|
lines=$(echo "$content" | wc -l)
|
||||||
|
echo "$content"
|
||||||
|
|
||||||
|
[ "$lines" -lt 100 ] && break
|
||||||
|
page=$((page + 1))
|
||||||
|
done | jq -s -c 'sort_by(.id) | .[]'
|
||||||
|
echo >&2
|
||||||
Loading…
Add table
Add a link
Reference in a new issue