Compare commits
4 commits
33ad6302da
...
cc87c4b30c
| Author | SHA1 | Date | |
|---|---|---|---|
| cc87c4b30c | |||
| 910ac3423e | |||
| ec5559f877 | |||
| 8a95ea80a7 |
6 changed files with 3931 additions and 75 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
|
||||||
|
|
|
||||||
47
create.sh
47
create.sh
|
|
@ -1,21 +1,52 @@
|
||||||
#!/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(.frontendId == $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 'cache-control: max-age=0' \
|
||||||
|
-H 'priority: u=0, i' \
|
||||||
|
-H 'sec-ch-ua: "Not:A-Brand";v="99", "Brave";v="145", "Chromium";v="145"' \
|
||||||
|
-H 'sec-ch-ua-mobile: ?0' \
|
||||||
|
-H 'sec-ch-ua-platform: "Linux"' \
|
||||||
|
-H 'sec-fetch-dest: document' \
|
||||||
|
-H 'sec-fetch-mode: navigate' \
|
||||||
|
-H 'sec-fetch-site: same-origin' \
|
||||||
|
-H 'sec-fetch-user: ?1' \
|
||||||
|
-H 'sec-gpc: 1' \
|
||||||
|
-H 'upgrade-insecure-requests: 1' \
|
||||||
|
-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."
|
||||||
|
|
|
||||||
3851
problemset.jsonl
Normal file
3851
problemset.jsonl
Normal file
File diff suppressed because it is too large
Load diff
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, frontendId: .questionFrontendId | tonumber, 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
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
package q134
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_canCompleteCircuit(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string // description of this test case
|
|
||||||
// Named input parameters for target function.
|
|
||||||
gas []int
|
|
||||||
cost []int
|
|
||||||
want int
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
gas: []int{1, 2, 3, 4, 5},
|
|
||||||
cost: []int{3, 4, 5, 1, 2},
|
|
||||||
want: 3,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
gas: []int{2, 3, 4},
|
|
||||||
cost: []int{3, 4, 3},
|
|
||||||
want: -1,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
got := canCompleteCircuit(tt.gas, tt.cost)
|
|
||||||
if got != tt.want {
|
|
||||||
t.Errorf("canCompleteCircuit() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
package q238
|
|
||||||
|
|
||||||
import (
|
|
||||||
"slices"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_productExceptSelf(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string // description of this test case
|
|
||||||
// Named input parameters for target function.
|
|
||||||
nums []int
|
|
||||||
want []int
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
nums: []int{1, 2, 3, 4},
|
|
||||||
want: []int{24, 12, 8, 6},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
nums: []int{-1, 1, 0, -3, 3},
|
|
||||||
want: []int{0, 0, 9, 0, 0},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
got := productExceptSelf(tt.nums)
|
|
||||||
if !slices.Equal(got, tt.want) {
|
|
||||||
t.Errorf("productExceptSelf() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue