8000 Usage examples for README · fspv/python-leetcode@f671850 · GitHub
[go: up one dir, main page]

Skip to content

Commit f671850

Browse files
committed
Usage examples for README
1 parent 375e169 commit f671850

File tree

1 file changed

+150
-4
lines changed

1 file changed

+150
-4
lines changed

README.md

Lines changed: 150 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,157 @@ which is needed in order to use LC API. If you use python you can
2626
just use the generated code from this repo. Otherwise you'll have to
2727
implement something like `fix_cookies.patch` for your target language.
2828

29-
30-
You can find examples of usage in `example.py`
31-
29+
## Minimal working example
30+
31+
First set up a virtualenv
32+
```bash
33+
virtualenv -p python3 leetcode
34+
. leetcode/bin/activate
35+
pip3 install python-leetcode
36+
```
37+
38+
Then in python shell initialize the client
39+
```python
40+
import leetcode
41+
42+
# Get the next two values from your browser cookies
43+
csrf_token = "xxx"
44+
leetcode_session = "yyy"
45+
46+
configuration = leetcode.Configuration()
47+
48+
configuration.api_key["x-csrftoken"] = csrf_token
49+
configuration.api_key["csrftoken"] = csrf_token
50+
configuration.api_key["LEETCODE_SESSION"] = leetcode_session
51+
configuration.api_key["Referer"] = "https://leetcode.com"
52+
configuration.debug = False
53+
54+
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
55+
```
56+
57+
Now once the client is initilized, you can start performing actual queries
58+
59+
```python
60+
graphql_request = leetcode.GraphqlQuery(
61+
query="""
62+
{
63+
user {
64+
username
65+
isCurrentUserPremium
66+
}
67+
}
68+
""",
69+
variables=leetcode.GraphqlQueryVariables(),
70+
)
71+
72+
print(api_instance.graphql_post(body=graphql_request))
73+
```
74+
75+
You should get something like that in the response
76+
```python
77+
{'data': {'question': None,
78+
'user': {'is_current_user_premium': True, 'username': 'omgitspavel'}}}
79+
```
80+
81+
This confirms you've set up auth correctly.
82+
83+
## Advanced example
84+
85+
Now let's try to do something more complicated. For example calculate the percentage of the problems we've solved by topic.
86+
87+
For that we have to acquire the list of all the problems we solved.
88+
89+
```python
90+
api_response = api_instance.api_problems_topic_get(topic="algorithms")
91+
92+
slug_to_solved_status = {
93+
pair.stat.question__title_slug: True if pair.status == "ac" else False
94+
for pair in api_response.stat_status_pairs
95+
}
96+
```
97+
98+
Now for each problem we want to get its tags
99+
100+
```python
101+
import time
102+
103+
from collections import Counter
104+
105+
106+
topic_to_accepted = Counter()
107+
topic_to_total = Counter()
108+
109+
110+
# Take only the first 10 for test purposes
111+
for slug in list(slug_to_solved_status.keys())[:10]:
112+
time.sleep(1) # Leetcode has a rate limiter
113+
114+
graphql_request = leetcode.GraphqlQuery(
115+
query="""
116+
query getQuestionDetail($titleSlug: String!) {
117+
question(titleSlug: $titleSlug) {
118+
topicTags {
119+
name
120+
slug
121+
}
122+
}
123+
}
124+
""",
125+
variables=leetcode.GraphqlQueryVariables(title_slug=slug),
126+
operation_name="getQuestionDetail",
127+
)
128+
129+
api_response = api_instance.graphql_post(body=graphql_request)
130+
131+
for topic in (tag.slug for tag in api_response.data.question.topic_tags):
132+
topic_to_accepted[topic] += int(slug_to_solved_status[slug])
133+
topic_to_total[topic] += 1
134+
135+
print(
136+
list(
137+
sorted(
138+
((topic, accepted / topic_to_total[topic]) for topic, accepted in topic_to_accepted.items()),
139+
key=lambda x: x[1]
140+
)
141+
)
142+
)
143+
```
144+
145+
The output will look like this:
146+
147+
```python
148+
[('memoization', 0.0),
149+
('number-theory', 0.0),
150+
('binary-search-tree', 0.0),
151+
('quickselect', 0.0),
152+
('recursion', 0.0),
153+
('suffix-array', 0.0),
154+
('topological-sort', 0.0),
155+
('shortest-path', 0.0),
156+
('trie', 0.0),
157+
('geometry', 0.0),
158+
('brainteaser', 0.0),
159+
('combinatorics', 0.0),
160+
('line-sweep', 0.0),
161+
162+
...
163+
164+
('union-find', 0.3076923076923077),
165+
('linked-list', 0.3333333333333333),
166+
('string-matching', 0.3333333333333333),
167+
('segment-tree', 0.4),
168+
('data-stream', 0.5),
169+
('strongly-connected-component', 0.5),
170+
('minimum-spanning-tree', 0.6666666666666666),
171+
('merge-sort', 1.0),
172+
('doubly-linked-list', 1.0)]
173+
```
174+
175+
So it is clearly visible which topics we should focus on in our preparation.
176+
In this case memoization topic is one of the targets for improvement, so I can go to https://leetcode.com/tag/memoization/ and choose a new memoization problem. Or use python to automate the process.
177+
178+
You can find other examples of usage in `example.py`
32179

33180
Autogenerated by swagger documentation can be found [here](/README.generated.md).
34181

35-
36182
[1] https://github.com/skygragon/leetcode-cli

0 commit comments

Comments
 (0)
0