forked from UPEISMCSCCC/kattis-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand.py
executable file
·102 lines (76 loc) · 2.64 KB
/
rand.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
import sys
import random
import argparse
def get_problem_difficulty(problem):
tds = problem.find_all('td')
text = tds[6].text.split('.')
return float(text[0] + '.' + text[1][:1])
try:
import requests
except ImportError:
sys.exit("You need requests. run 'pip install requests'")
try:
from bs4 import BeautifulSoup
except ImportError:
sys.exit("You need BeautifulSoup. run 'pip install bs4'")
try:
from fake_useragent import UserAgent
except ImportError:
sys.exit("You need UserAgent. run 'pip install fake-useragent'")
parser = argparse.ArgumentParser(description='Runs Kattis problem through their test cases')
parser.add_argument('-n', type=int, default=None, help='the amount of problems wanted to fetch')
parser.add_argument('-l', type=float, default=None, help='the lower bound for problems')
parser.add_argument('-u', type=float, default=None, help='the upper bound for problems')
args = parser.parse_args()
n= args.n
lower_bound = args.l
upper_bound = args.u
if n is None:
n = int(input('How many problems: '))
if lower_bound is None:
lower_bound = float(input('Enter lower bound: '))
if upper_bound is None:
upper_bound = float(input('Enter upper bound: '))
tmp = upper_bound
upper_bound = min(max(upper_bound, lower_bound), 10)
lower_bound = max(min(lower_bound, tmp), 0)
seenlist = []
with open('./seen.txt') as f:
seenlist = [line.rstrip('\n') for line in f]
qlist = []
done = False
i = 0
while not done:
url = 'https://open.kattis.com/problems?page=' + str(i) + '&order=problem_difficulty'
usa = UserAgent()
page = requests.get(url, headers={'User-Agent':str(usa.random)})
soup = BeautifulSoup(page.content, 'html.parser')
table = soup.find('table', attrs={'class': 'table2'})
if table is None:
done = True
tbody = table.find('tbody')
problems = tbody.find_all('tr')
if problems:
greatest_diff = get_problem_difficulty(problems[-1])
if greatest_diff < lower_bound:
i += 1
continue
for problem in problems:
tds = problem.find_all('td')
try:
diff = get_problem_difficulty(problem)
except ValueError:
continue
if diff > upper_bound:
done = True
elif diff >= lower_bound and diff <= upper_bound:
qid_url = tds[0].find('a')['href']
if qid_url.split('/')[2] not in seenlist:
qlist.append(qid_url.split('/')[2])
i += 1
random.shuffle(qlist)
qlist = qlist[:n]
print(f'Here are {n} Kattis problems: {" ".join(qlist)}')
with open('seen.txt', 'a+') as f:
f.write('\n'.join(qlist) + '\n')