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

Added CodeForces Scraper #68

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Scripts/Web_Scrappers/CodeForces_Scraper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# CodeForces Problem Scraper

Takes A CodeForces Problem Code As A Command-Line Argument, Scrapes The Appropriate Problem, And Saves It To A Text File.

## Pre-Requisites

Run The Command

`pip install -r requirements.txt`

## Instructions To Run

Run The Command

`python codeforces_scraper.py {CODEFORCES-PROBLEM-CODE}`

## Screenshot - Sample Use

![Screenshot](screenshot.png)

## *Author Name*
[Roopesh V S](https://github.com/roopeshvs)
54 changes: 54 additions & 0 deletions Scripts/Web_Scrappers/CodeForces_Scraper/codeforces_scraper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from bs4 import BeautifulSoup
import requests
import sys


def get_problem_statement(problem_code):
"""
This function takes a Codeforces problem code as input and
scrapes the problem statement from the site and returns
the parsed problem statement.

Args:
problem_code (string): CodeForces Problem Code
Returns:
problem_statement (string): CodeForces Problem
"""
problem_number = problem_code[:-1]
problem_letter = problem_code[-1]
URL = f"https://codeforces.com/problemset/problem/\
{problem_number}/{problem_letter}"
try:
page = requests.get(URL)
if not page:
raise Exception(page.status_code)
except Exception as e:
print("Cannot Find CodeForces Problem!" + str(e))
exit(0)
soup = BeautifulSoup(page.content, 'html.parser')
soup.find('div', class_='header').decompose()
problem_statement_div = soup.find('div', class_="problem-statement")
response = problem_statement_div.text.replace("$$$", "")
return response


def to_txt(problem_code, problem):
"""
Takes A Problem Code & Its Appropriate Parsed CodeForces Problem And
Prints It To A Text File.
"""
with open(problem_code + '.txt', 'w') as output_file:
output_file.writelines(problem)


if __name__ == "__main__":
try:
problem_code = sys.argv[1]
except Exception:
print('Please Enter A CodeForces Problem Code as a',
'Command-Line Argument!')
exit(0)
problem = get_problem_statement(problem_code)
to_txt(problem_code, problem)
print(f'Problem {problem_code} Successfully Scraped And Saved To',
f'{problem_code}.txt')
2 changes: 2 additions & 0 deletions Scripts/Web_Scrappers/CodeForces_Scraper/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bs4
requests
24 changes: 24 additions & 0 deletions Scripts/Web_Scrappers/CodeForces_Scraper/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
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].
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.