-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode.py
34 lines (27 loc) · 1.05 KB
/
leetcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import requests
import bs4
import json
import random
class Leetcode:
r = requests.get(f"https://leetcode.com/api/problems/algorithms/").text
soup = bs4.BeautifulSoup(r, 'html.parser')
json_output = json.loads(soup.text)
questions=json_output['stat_status_pairs']
def randomQuestion(self):
problems = []
for question in self.questions:
problems.append(question['stat'])
x = random.randint(0, len(problems)-1)
return f"https://leetcode.com/problems/{problems[x]['question__title_slug']}"
def getByName(self,name,maxoutput=1):
list_of_questions=[]
terms=list(name.split('-'))
for question in self.questions:
for term in terms:
if term in question['stat']['question__title']:
list_of_questions.append(
f"https://leetcode.com/problems/{question['stat']['question__title_slug']}")
random.shuffle(list_of_questions)
return list_of_questions[:maxoutput]
v=Leetcode()
print(v.getByName('BST'))