Skip to content

Instantly share code, notes, and snippets.

@akshaykarnawat
Created March 22, 2019 03:42
Show Gist options
  • Save akshaykarnawat/6479579a351f6fe52aab4042829ca967 to your computer and use it in GitHub Desktop.
Save akshaykarnawat/6479579a351f6fe52aab4042829ca967 to your computer and use it in GitHub Desktop.
Get LeetCode problems data saved locally for offline access
import requests
import json
import time
import os
LOC_TO_STORE = '/Volumes/ExPac/dev/python/leet_code_problems'
LEET_CODE_URL = 'https://leetcode.com'
LEET_CODE_GRAPHQL_URL = 'https://leetcode.com/graphql'
# get all the problems and take only the needed information
request = requests.get('/'.join([LEET_CODE_URL, 'api/problems/all/']))
data = json.loads(request.text)
problems = {}
_ = [problems.setdefault(p.get('difficulty', {}).get('level', ''), {}).setdefault(p.get('stat', {}).get('question__title_slug'),
'/'.join([LEET_CODE_URL, 'problems', p.get('stat', {}).get('question__title_slug')]))
for p in data.get('stat_status_pairs')]
# create a payload to send to graph ql to get the questions data
payload = {
"operationName" : "questionData",
"variables" : {"titleSlug":"two-sum"},
"query" : '''\
query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
title
titleSlug
content
}
}
''',
}
HEX_LIKE_URL_1 = '''https://leetcode.com/graphql?operationName=questionData&variables={%22titleSlug%22:%22'''
HEX_LIKE_URL_2 = '''%22}&query=query%20questionData($titleSlug:%20String!)%20{%20%20%20%20%20question(titleSlug:%20$titleSlug)%20{%20%20%20%20%20%20%20%20%20titleSlug%20%20%20%20%20%20%20%20%20content%20%20%20%20%20}%20}'''
# get to the problem one by one and start downloading based on difficulty
# 1 = EASY; 2 = MEDIUM; 3 = HARD
for difficulty in problems:
# create a new folder in local path
if not os.path.exists('/'.join([LOC_TO_STORE, str(difficulty)])):
os.makedirs('/'.join([LOC_TO_STORE, str(difficulty)]))
for title, pUrl in problems.get(difficulty).items():
res = requests.get(HEX_LIKE_URL_1 + title + HEX_LIKE_URL_2)
if res.status_code != 200:
continue
d = json.loads(res.text)
quesData = d.get('data', {}).get('question', {})
titleSlug = quesData.get('titleSlug', '')
content = quesData.get('content', '')
# save data to local folder
with open('{0}/{1}/{2}.html'.format(LOC_TO_STORE, str(difficulty), titleSlug), 'w+') as f:
try:
f.writelines(content)
f.close()
except Exception as e:
print(title, 'had a problem while saving!', e.args[0])
# maybe wait a little so that server does seem like there is something fishy with all the pulls
# although I doubt this even matters since 30 sec is not enough of a wait; they will know regardless
# since we are pulling everything all
# time.sleep(30)
(test) /Volumes/ExPac/dev/python/leet_code_problems > python leet_code_problems.py
robot-room-cleaner had a problem while saving! 'NoneType' object is not iterable
minimize-max-distance-to-gas-station had a problem while saving! 'NoneType' object is not iterable
basic-calculator-iii had a problem while saving! 'NoneType' object is not iterable
encode-n-ary-tree-to-binary-tree had a problem while saving! 'NoneType' object is not iterable
serialize-and-deserialize-n-ary-tree had a problem while saving! 'NoneType' object is not iterable
employee-free-time had a problem while saving! 'NoneType' object is not iterable
minimum-window-subsequence had a problem while saving! 'NoneType' object is not iterable
number-of-distinct-islands-ii had a problem while saving! 'NoneType' object is not iterable
k-empty-slots had a problem while saving! 'NoneType' object is not iterable
remove-9 had a problem while saving! 'NoneType' object is not iterable
coin-path had a problem while saving! 'NoneType' object is not iterable
maximum-average-subarray-ii had a problem while saving! 'NoneType' object is not iterable
design-search-autocomplete-system had a problem while saving! 'NoneType' object is not iterable
design-excel-sum-formula had a problem while saving! 'NoneType' object is not iterable
students-report-by-geography had a problem while saving! 'NoneType' object is not iterable
average-salary-departments-vs-company had a problem while saving! 'NoneType' object is not iterable
design-in-memory-file-system had a problem while saving! 'NoneType' object is not iterable
find-cumulative-salary-of-an-employee had a problem while saving! 'NoneType' object is not iterable
find-median-given-frequency-of-numbers had a problem while saving! 'NoneType' object is not iterable
median-employee-salary had a problem while saving! 'NoneType' object is not iterable
maximum-vacation-days had a problem while saving! 'NoneType' object is not iterable
word-abbreviation had a problem while saving! 'NoneType' object is not iterable
the-maze-iii had a problem while saving! 'NoneType' object is not iterable
encode-string-with-shortest-length had a problem while saving! 'NoneType' object is not iterable
optimal-account-balancing had a problem while saving! 'NoneType' object is not iterable
word-squares had a problem while saving! 'NoneType' object is not iterable
minimum-unique-word-abbreviation had a problem while saving! 'NoneType' object is not iterable
rearrange-string-k-distance-apart had a problem while saving! 'NoneType' object is not iterable
longest-substring-with-at-most-k-distinct-characters had a problem while saving! 'NoneType' object is not iterable
shortest-distance-from-all-buildings had a problem while saving! 'NoneType' object is not iterable
range-sum-query-2d-mutable had a problem while saving! 'NoneType' object is not iterable
number-of-islands-ii had a problem while saving! 'NoneType' object is not iterable
smallest-rectangle-enclosing-black-pixels had a problem while saving! 'NoneType' object is not iterable
best-meeting-point had a problem while saving! 'NoneType' object is not iterable
word-pattern-ii had a problem while saving! 'NoneType' object is not iterable
closest-binary-search-tree-value-ii had a problem while saving! 'NoneType' object is not iterable
alien-dictionary had a problem while saving! 'NoneType' object is not iterable
paint-house-ii had a problem while saving! 'NoneType' object is not iterable
strobogrammatic-number-iii had a problem while saving! 'NoneType' object is not iterable
longest-substring-with-at-most-two-distinct-characters had a problem while saving! 'NoneType' object is not iterable
read-n-characters-given-read4-ii-call-multiple-times had a problem while saving! 'NoneType' object is not iterable
insert-into-a-cyclic-sorted-list had a problem while saving! 'NoneType' object is not iterable
split-bst had a problem while saving! 'NoneType' object is not iterable
search-in-a-sorted-array-of-unknown-size had a problem while saving! 'NoneType' object is not iterable
convert-binary-search-tree-to-sorted-doubly-linked-list had a problem while saving! 'NoneType' object is not iterable
pour-water had a problem while saving! 'NoneType' object is not iterable
number-of-corner-rectangles had a problem while saving! 'NoneType' object is not iterable
closest-leaf-in-a-binary-tree had a problem while saving! 'NoneType' object is not iterable
sentence-similarity-ii had a problem while saving! 'NoneType' object is not iterable
candy-crush had a problem while saving! 'NoneType' object is not iterable
number-of-distinct-islands had a problem while saving! 'NoneType' object is not iterable
next-closest-time had a problem while saving! 'NoneType' object is not iterable
path-sum-iv had a problem while saving! 'NoneType' object is not iterable
equal-tree-partition had a problem while saving! 'NoneType' object is not iterable
4-keys-keyboard had a problem while saving! 'NoneType' object is not iterable
design-log-storage-system had a problem while saving! 'NoneType' object is not iterable
find-the-derangement-of-an-array had a problem while saving! 'NoneType' object is not iterable
minimum-factorization had a problem while saving! 'NoneType' object is not iterable
add-bold-tag-in-string had a problem while saving! 'NoneType' object is not iterable
second-degree-follower had a problem while saving! 'NoneType' object is not iterable
shortest-distance-in-a-plane had a problem while saving! 'NoneType' object is not iterable
tree-node had a problem while saving! 'NoneType' object is not iterable
friend-requests-ii-who-has-the-most-friends had a problem while saving! 'NoneType' object is not iterable
investments-in-2016 had a problem while saving! 'NoneType' object is not iterable
kill-process had a problem while saving! 'NoneType' object is not iterable
count-student-number-in-departments had a problem while saving! 'NoneType' object is not iterable
get-highest-answer-rate-question had a problem while saving! 'NoneType' object is not iterable
winning-candidate had a problem while saving! 'NoneType' object is not iterable
squirrel-simulation had a problem while saving! 'NoneType' object is not iterable
managers-with-at-least-5-direct-reports had a problem while saving! 'NoneType' object is not iterable
longest-line-of-consecutive-one-in-matrix had a problem while saving! 'NoneType' object is not iterable
split-concatenated-strings had a problem while saving! 'NoneType' object is not iterable
binary-tree-longest-consecutive-sequence-ii had a problem while saving! 'NoneType' object is not iterable
split-array-with-equal-sum had a problem while saving! 'NoneType' object is not iterable
boundary-of-binary-tree had a problem while saving! 'NoneType' object is not iterable
output-contest-matches had a problem while saving! 'NoneType' object is not iterable
construct-binary-tree-from-string had a problem while saving! 'NoneType' object is not iterable
lonely-pixel-ii had a problem while saving! 'NoneType' object is not iterable
lonely-pixel-i had a problem while saving! 'NoneType' object is not iterable
inorder-successor-in-bst-ii had a problem while saving! 'NoneType' object is not iterable
the-maze-ii had a problem while saving! 'NoneType' object is not iterable
the-maze had a problem while saving! 'NoneType' object is not iterable
max-consecutive-ones-ii had a problem while saving! 'NoneType' object is not iterable
find-permutation had a problem while saving! 'NoneType' object is not iterable
convex-polygon had a problem while saving! 'NoneType' object is not iterable
sequence-reconstruction had a problem while saving! 'NoneType' object is not iterable
ternary-expression-parser had a problem while saving! 'NoneType' object is not iterable
sentence-screen-fitting had a problem while saving! 'NoneType' object is not iterable
design-phone-directory had a problem while saving! 'NoneType' object is not iterable
range-addition had a problem while saving! 'NoneType' object is not iterable
plus-one-linked-list had a problem while saving! 'NoneType' object is not iterable
find-leaves-of-binary-tree had a problem while saving! 'NoneType' object is not iterable
nested-list-weight-sum-ii had a problem while saving! 'NoneType' object is not iterable
design-hit-counter had a problem while saving! 'NoneType' object is not iterable
bomb-enemy had a problem while saving! 'NoneType' object is not iterable
sort-transformed-array had a problem while saving! 'NoneType' object is not iterable
line-reflection had a problem while saving! 'NoneType' object is not iterable
design-snake-game had a problem while saving! 'NoneType' object is not iterable
android-unlock-patterns had a problem while saving! 'NoneType' object is not iterable
design-tic-tac-toe had a problem while saving! 'NoneType' object is not iterable
largest-bst-subtree had a problem while saving! 'NoneType' object is not iterable
maximum-size-subarray-sum-equals-k had a problem while saving! 'NoneType' object is not iterable
number-of-connected-components-in-an-undirected-graph had a problem while saving! 'NoneType' object is not iterable
generalized-abbreviation had a problem while saving! 'NoneType' object is not iterable
binary-tree-vertical-order-traversal had a problem while saving! 'NoneType' object is not iterable
sparse-matrix-multiplication had a problem while saving! 'NoneType' object is not iterable
binary-tree-longest-consecutive-sequence had a problem while saving! 'NoneType' object is not iterable
flip-game-ii had a problem while saving! 'NoneType' object is not iterable
unique-word-abbreviation had a problem while saving! 'NoneType' object is not iterable
walls-and-gates had a problem while saving! 'NoneType' object is not iterable
inorder-successor-in-bst had a problem while saving! 'NoneType' object is not iterable
zigzag-iterator had a problem while saving! 'NoneType' object is not iterable
wiggle-sort had a problem while saving! 'NoneType' object is not iterable
find-the-celebrity had a problem while saving! 'NoneType' object is not iterable
encode-and-decode-strings had a problem while saving! 'NoneType' object is not iterable
palindrome-permutation-ii had a problem while saving! 'NoneType' object is not iterable
graph-valid-tree had a problem while saving! 'NoneType' object is not iterable
3sum-smaller had a problem while saving! 'NoneType' object is not iterable
verify-preorder-sequence-in-binary-search-tree had a problem while saving! 'NoneType' object is not iterable
factor-combinations had a problem while saving! 'NoneType' object is not iterable
meeting-rooms-ii had a problem while saving! 'NoneType' object is not iterable
flatten-2d-vector had a problem while saving! 'NoneType' object is not iterable
count-univalue-subtrees had a problem while saving! 'NoneType' object is not iterable
group-shifted-strings had a problem while saving! 'NoneType' object is not iterable
strobogrammatic-number-ii had a problem while saving! 'NoneType' object is not iterable
shortest-word-distance-iii had a problem while saving! 'NoneType' object is not iterable
shortest-word-distance-ii had a problem while saving! 'NoneType' object is not iterable
reverse-words-in-a-string-ii had a problem while saving! 'NoneType' object is not iterable
missing-ranges had a problem while saving! 'NoneType' object is not iterable
one-edit-distance had a problem while saving! 'NoneType' object is not iterable
binary-tree-upside-down had a problem while saving! 'NoneType' object is not iterable
similar-rgb-color had a problem while saving! 'NoneType' object is not iterable
find-anagram-mappings had a problem while saving! 'NoneType' object is not iterable
bold-words-in-string had a problem while saving! 'NoneType' object is not iterable
ip-to-cidr had a problem while saving! 'NoneType' object is not iterable
sentence-similarity had a problem while saving! 'NoneType' object is not iterable
max-stack had a problem while saving! 'NoneType' object is not iterable
maximum-distance-in-arrays had a problem while saving! 'NoneType' object is not iterable
biggest-single-number had a problem while saving! 'NoneType' object is not iterable
shortest-distance-in-a-line had a problem while saving! 'NoneType' object is not iterable
triangle-judgement had a problem while saving! 'NoneType' object is not iterable
sales-person had a problem while saving! 'NoneType' object is not iterable
design-compressed-string-iterator had a problem while saving! 'NoneType' object is not iterable
consecutive-available-seats had a problem while saving! 'NoneType' object is not iterable
friend-requests-i-overall-acceptance-rate had a problem while saving! 'NoneType' object is not iterable
customer-placing-the-largest-number-of-orders had a problem while saving! 'NoneType' object is not iterable
find-customer-referee had a problem while saving! 'NoneType' object is not iterable
employee-bonus had a problem while saving! 'NoneType' object is not iterable
valid-word-square had a problem while saving! 'NoneType' object is not iterable
valid-word-abbreviation had a problem while saving! 'NoneType' object is not iterable
logger-rate-limiter had a problem while saving! 'NoneType' object is not iterable
moving-average-from-data-stream had a problem while saving! 'NoneType' object is not iterable
nested-list-weight-sum had a problem while saving! 'NoneType' object is not iterable
flip-game had a problem while saving! 'NoneType' object is not iterable
paint-fence had a problem while saving! 'NoneType' object is not iterable
closest-binary-search-tree-value had a problem while saving! 'NoneType' object is not iterable
palindrome-permutation had a problem while saving! 'NoneType' object is not iterable
paint-house had a problem while saving! 'NoneType' object is not iterable
meeting-rooms had a problem while saving! 'NoneType' object is not iterable
strobogrammatic-number had a problem while saving! 'NoneType' object is not iterable
shortest-word-distance had a problem while saving! 'NoneType' object is not iterable
two-sum-iii-data-structure-design had a problem while saving! 'NoneType' object is not iterable
read-n-characters-given-read4 had a problem while saving! 'NoneType' object is not iterable
<p>Given an array of integers, return <strong>indices</strong> of the two numbers such that they add up to a specific target.</p>
<p>You may assume that each input would have <strong><em>exactly</em></strong> one solution, and you may not use the <em>same</em> element twice.</p>
<p><strong>Example:</strong></p>
<pre>
Given nums = [2, 7, 11, 15], target = 9,
Because nums[<strong>0</strong>] + nums[<strong>1</strong>] = 2 + 7 = 9,
return [<strong>0</strong>, <strong>1</strong>].
</pre>
<p>&nbsp;</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment