ymcmy commited on
Commit
598ca45
·
verified ·
1 Parent(s): f03d8b8

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +43 -39
utils.py CHANGED
@@ -5,30 +5,28 @@ from datetime import datetime
5
 
6
  def gen_link():
7
  if(np.random.choice([True, False])):
8
- #amc
9
  np.random.seed()
10
- year=np.random.randint(2015,2023)
11
- AB=np.random.choice(['A', 'B'])
12
- #question
13
- mu,sigma=18, 5
14
- s=np.random.normal(mu,sigma,1000)
15
  s = np.round(s)
16
- s=s[s>=10]
17
- s=s[s<=25]
18
- q=int(np.random.choice(s))
19
- link='https://artofproblemsolving.com/wiki/index.php/{}_AMC_12{}_Problems/Problem_{}'.format(year, AB, q)
20
  else:
21
- #aime
22
  np.random.seed()
23
- year=np.random.randint(2005,2023)
24
- I=np.random.choice(['I', 'II'])
25
- mu,sigma=6, 4
26
- s=np.random.normal(mu,sigma,1000)
27
  s = np.round(s)
28
- s=s[s>=1]
29
- s=s[s<=15]
30
- q=int(np.random.choice(s))
31
- link='https://artofproblemsolving.com/wiki/index.php/{}_AIME_{}_Problems/Problem_{}'.format(year,I, q)
32
  return link
33
 
34
  def convert_to_renderable_html(text):
@@ -37,54 +35,60 @@ def convert_to_renderable_html(text):
37
 
38
  def get_problem(url):
39
  headers = {
40
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
41
  }
42
  response = requests.get(url, headers=headers)
43
  soup = BeautifulSoup(response.text, 'html.parser')
44
  problem_headline = soup.find('span', {'class': 'mw-headline', 'id': 'Problem'})
45
-
46
  if problem_headline:
47
  problem_content = []
48
  for sibling in problem_headline.parent.find_next_siblings():
49
- # If the sibling is a headline ('h2'), break the loop, as we've reached the next section
50
  if sibling.name == 'h2':
51
  break
52
- # If the sibling is a paragraph ('p'), add it to the problem content
53
  elif sibling.name == 'p':
54
  problem_content.append(convert_to_renderable_html(str(sibling)))
55
 
56
- # Join all paragraphs into a single string (HTML)
57
  problem_html = " ".join(problem_content)
58
  return problem_html
59
  else:
60
  print("No problem found")
61
 
62
  def gen_html(num):
63
- all_q=str()
64
- num_tried=0
65
- num_succ=0
66
- while(True):
67
  try:
68
- link=gen_link()
69
  print(link)
70
- hype = '<a href="{}" target="_blank">to link</a>'.format(link)
71
- qhtml=get_problem(link)
72
- all_q+=(hype+qhtml)
73
- num_succ+=1
74
- except:
 
75
  pass
76
- num_tried+=1
77
- if num_succ>=num or num_tried>20:
78
  break
79
 
80
- all_q=f'''
81
  <html>
82
  <head>
 
 
 
 
 
 
 
 
 
83
  </head>
84
  <body>
85
- {all_q}
86
  </body>
87
  </html>
88
  '''
89
-
90
  return all_q
 
5
 
6
  def gen_link():
7
  if(np.random.choice([True, False])):
8
+ # AMC
9
  np.random.seed()
10
+ year = np.random.randint(2015, 2023)
11
+ AB = np.random.choice(['A', 'B'])
12
+ # Question
13
+ mu, sigma = 18, 5
14
+ s = np.random.normal(mu, sigma, 1000)
15
  s = np.round(s)
16
+ s = s[(s >= 10) & (s <= 25)]
17
+ q = int(np.random.choice(s))
18
+ link = f'https://artofproblemsolving.com/wiki/index.php/{year}_AMC_12{AB}_Problems/Problem_{q}'
 
19
  else:
20
+ # AIME
21
  np.random.seed()
22
+ year = np.random.randint(2005, 2023)
23
+ I = np.random.choice(['I', 'II'])
24
+ mu, sigma = 6, 4
25
+ s = np.random.normal(mu, sigma, 1000)
26
  s = np.round(s)
27
+ s = s[(s >= 1) & (s <= 15)]
28
+ q = int(np.random.choice(s))
29
+ link = f'https://artofproblemsolving.com/wiki/index.php/{year}_AIME_{I}_Problems/Problem_{q}'
 
30
  return link
31
 
32
  def convert_to_renderable_html(text):
 
35
 
36
  def get_problem(url):
37
  headers = {
38
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
39
  }
40
  response = requests.get(url, headers=headers)
41
  soup = BeautifulSoup(response.text, 'html.parser')
42
  problem_headline = soup.find('span', {'class': 'mw-headline', 'id': 'Problem'})
43
+
44
  if problem_headline:
45
  problem_content = []
46
  for sibling in problem_headline.parent.find_next_siblings():
 
47
  if sibling.name == 'h2':
48
  break
 
49
  elif sibling.name == 'p':
50
  problem_content.append(convert_to_renderable_html(str(sibling)))
51
 
 
52
  problem_html = " ".join(problem_content)
53
  return problem_html
54
  else:
55
  print("No problem found")
56
 
57
  def gen_html(num):
58
+ all_q = str()
59
+ num_tried = 0
60
+ num_succ = 0
61
+ while True:
62
  try:
63
+ link = gen_link()
64
  print(link)
65
+ hype = f'<a href="{link}" target="_blank">to link</a>'
66
+ qhtml = get_problem(link)
67
+ all_q += (hype + qhtml)
68
+ num_succ += 1
69
+ except Exception as e:
70
+ print(f"Error: {e}")
71
  pass
72
+ num_tried += 1
73
+ if num_succ >= num or num_tried > 20:
74
  break
75
 
76
+ all_q = f'''
77
  <html>
78
  <head>
79
+ <style>
80
+ body {{
81
+ font-family: Arial, sans-serif;
82
+ font-size: 12pt;
83
+ }}
84
+ .math {{
85
+ font-size: 12pt; /* Ensure math font size matches the body text size */
86
+ }}
87
+ </style>
88
  </head>
89
  <body>
90
+ {all_q}
91
  </body>
92
  </html>
93
  '''
 
94
  return all_q