Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit eee2d6e

Browse files
authored
Merge pull request #68 from roopeshvs/master
Added CodeForces Scraper
2 parents b98c521 + 3cd7830 commit eee2d6e

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# CodeForces Problem Scraper
2+
3+
Takes A CodeForces Problem Code As A Command-Line Argument, Scrapes The Appropriate Problem, And Saves It To A Text File.
4+
5+
## Pre-Requisites
6+
7+
Run The Command
8+
9+
`pip install -r requirements.txt`
10+
11+
## Instructions To Run
12+
13+
Run The Command
14+
15+
`python codeforces_scraper.py {CODEFORCES-PROBLEM-CODE}`
16+
17+
## Screenshot - Sample Use
18+
19+
![Screenshot](screenshot.png)
20+
21+
## *Author Name*
22+
[Roopesh V S](https://github.com/roopeshvs)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from bs4 import BeautifulSoup
2+
import requests
3+
import sys
4+
5+
6+
def get_problem_statement(problem_code):
7+
"""
8+
This function takes a Codeforces problem code as input and
9+
scrapes the problem statement from the site and returns
10+
the parsed problem statement.
11+
12+
Args:
13+
problem_code (string): CodeForces Problem Code
14+
Returns:
15+
problem_statement (string): CodeForces Problem
16+
"""
17+
problem_number = problem_code[:-1]
18+
problem_letter = problem_code[-1]
19+
URL = f"https://codeforces.com/problemset/problem/\
20+
{problem_number}/{problem_letter}"
21+
try:
22+
page = requests.get(URL)
23+
if not page:
24+
raise Exception(page.status_code)
25+
except Exception as e:
26+
print("Cannot Find CodeForces Problem!" + str(e))
27+
exit(0)
28+
soup = BeautifulSoup(page.content, 'html.parser')
29+
soup.find('div', class_='header').decompose()
30+
problem_statement_div = soup.find('div', class_="problem-statement")
31+
response = problem_statement_div.text.replace("$$$", "")
32+
return response
33+
34+
35+
def to_txt(problem_code, problem):
36+
"""
37+
Takes A Problem Code & Its Appropriate Parsed CodeForces Problem And
38+
Prints It To A Text File.
39+
"""
40+
with open(problem_code + '.txt', 'w') as output_file:
41+
output_file.writelines(problem)
42+
43+
44+
if __name__ == "__main__":
45+
try:
46+
problem_code = sys.argv[1]
47+
except Exception:
48+
print('Please Enter A CodeForces Problem Code as a',
49+
'Command-Line Argument!')
50+
exit(0)
51+
problem = get_problem_statement(problem_code)
52+
to_txt(problem_code, problem)
53+
print(f'Problem {problem_code} Successfully Scraped And Saved To',
54+
f'{problem_code}.txt')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bs4
2+
requests
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.During one move, you can choose some gift 1 \le i \le n and do one of the following operations: eat exactly one candy from this gift (decrease a_i by one); eat exactly one orange from this gift (decrease b_i by one); eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one). Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = \dots = a_n and b_1 = b_2 = \dots = b_n (and a_i equals b_i is not necessary).Your task is to find the minimum number of moves required to equalize all the given gifts.You have to answer t independent test cases.InputThe first line of the input contains one integer t (1 \le t \le 1000) � the number of test cases. Then t test cases follow.The first line of the test case contains one integer n (1 \le n \le 50) � the number of gifts. The second line of the test case contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, \dots, b_n (1 \le b_i \le 10^9), where b_i is the number of oranges in the i-th gift.OutputFor each test case, print one integer: the minimum number of moves required to equalize all the given gifts.ExampleInput
2+
5
3+
3
4+
3 5 6
5+
3 2 3
6+
5
7+
1 2 3 4 5
8+
5 4 3 2 1
9+
3
10+
1 1 1
11+
2 2 2
12+
6
13+
1 1000000000 1000000000 1000000000 1000000000 1000000000
14+
1 1 1 1 1 1
15+
3
16+
10 12 8
17+
7 5 4
18+
Output
19+
6
20+
16
21+
0
22+
4999999995
23+
7
24+
NoteIn the first test case of the example, we can perform the following sequence of moves: choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3]; choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3]; choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3]; choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2]; choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2]; choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2].
39.9 KB
Loading

0 commit comments

Comments
 (0)