Added Codeforces Checker Python Script (#176)

This commit is contained in:
Jinesh Parakh 2020-10-06 13:09:14 +05:30 committed by GitHub
parent 77f5f99296
commit 5e38bca219
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 222 additions and 0 deletions

View File

@ -153,6 +153,7 @@ So far, the following projects have been integrated to this repo:
|[Tambola_Ticket_Generator](Tambola_Ticket_Generator)|[Amandeep_Singh](https://github.com/Synster)|
| [Py_Cleaner](Py_Cleaner) | [Abhishek Dobliyal](https://github.com/Abhishek-Dobliyal)
|[Send messages to sqs in parallel](send_sqs_messages_in_parallel)|[Jinam Shah](https://github.com/jinamshah)|
|[Codeforces Checker](codeforcesChecker)|[Jinesh Parakh](https://github.com/jineshparakh)|
## How to use :

View File

@ -0,0 +1,23 @@
# codeforcesChecker
CodeforcesChecker is a useful tool for Competitive Programmers using the Codeforces website. <br>
The main motive of this project is to help users save time while solving the problems and reduce the hassels of checking validity of their sample outputs with the expected sample outputs.
Before using the script some dependencies need to be installed<br>
1. You need to have python3 installed on your system<br>
2. You need to have various modules installed viz. selenium, chromedriver_binary, beautifulSoup4, requests, shutil, filecmp, sys, termcolor, coloroma, os and any other dependencies absent on your system.
You can install these using pip3(Example: pip3 install chromedriver_binary )<br>
<b>To install all the dependencies you can simply type this command in your terminal: pip3 install -r requirements.txt</b>
Procedure to use the script: <br>
1. The first thing you need to do is to copy-paste or write your C++ template to the template.cpp file. (I have provided a sample template)<br>
2. After the template, you should run the main.py file by typing <b>python3 main.py</b> onto your terminal.<br>
3. Then you need to enter the URL of the site.<br>
Now URL's can be of two type<br>
3.1. If you need to clone the entire contest, may it be live or virtual or just upsolving.<br>
3.2. You only need a problem from the problemset.<br>
4. After the exection of main.py file a folder will be created either of the contest or the problem you wish to solve.Each problem of the contest will have a specific folder having the soln.cpp file(containing the template), the checker.py file and all the sample input and output files.<br>
5. You now need to change your current working directory to the Problem folder using <b>cd</b> command. After this you need to write your solution in the <b>soln.cpp</b> file, save it and then close it.<br>
6. Type <b>python3 checker.py</b> on your terminal and press Enter key. This script will check your code against sample inputs and sample outputs and give "VERDICT OK", if all the test cases pass or "VERDICT NOT OK", if either some or all testcases fail. For the testcases which fail, you will also get your output along with the expected output on the terminal screen. <br>
7. The similar process can be followed for all the problems.

View File

@ -0,0 +1,69 @@
"""
Written by: Jinesh Parakh
This is a checker script which compiles and runs the C++ code that you have written and checks it's accuracy with all the sample inputs and outputs scrapped using the main.py script
"""
#import necessary ibraries or modules
import os
import filecmp
import sys
from termcolor import colored, cprint
from colorama import Fore, Back, Style
import shutil
# The next two functions are used to format text for increasing the user experience
def prRed(skk): print("\033[91m {}\033[00m" .format(skk))
def prGreen(skk): print("\033[92m {}\033[00m" .format(skk))
def returnNumberofFiles(): #function to return number of files in that folder
path = os.getcwd()
numFiles = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))])
numFiles-=2
numFiles=numFiles//2
return numFiles
numFiles=returnNumberofFiles()
print("Compiling Code........")
if os.system("g++ soln.cpp")!=0: #Compiling the C++ code (soln.cpp)
cprint('Compilation Error', 'red',attrs=['bold'])
sys.exit()
cprint('Code successfully Compiled!!!!', 'green',attrs=['bold'])
flag=0
for i in range(0,numFiles):
os.system("./a.out<input"+str(i)+".txt>myoutput"+str(i)+".txt") #running the compiled C++ code
file1="output"+str(i)+".txt"
file2="myoutput"+str(i)+".txt"
file3="myoutputt"+str(i)+".txt"
shutil.copy(file2,file3)
f=open(file3,"a+")
f.write("\n")
f.close()
if filecmp.cmp(file1,file2) or filecmp.cmp(file1,file3): #Checking if the expected output matches with your output
print(Fore.GREEN + u'\u2714', end=' ')
prGreen("Sample Test Case "+str(i) +" PASS")
print()
Style.RESET_ALL
else: #if the expected output does not match
flag=1
print(Fore.RED + u'\u2718', end=' ')
prRed("Sample Test Case "+str(i) +" FAIL")
Style.RESET_ALL
print("Expected Output: ")
x=open(file1,"r+")
print(x.read())
print("Your Output: ")
y=open(file2,"r+")
print(y.read())
print()
os.remove(file2)
os.remove(file3)
os.remove("a.out")
if flag==0: #if all test cases pass, give OK VERDICT
cprint('All sample inputs passed!! VERDICT OK', 'green',attrs=['bold'])
else: #if some test cases do not pass, give NOT OK VERICT
cprint('Some or all sample inputs failed!! VERDICT NOT OK', 'red',attrs=['bold'])

123
codeforcesChecker/main.py Normal file
View File

@ -0,0 +1,123 @@
"""
Written By: Jinesh Parakh
This is the main script file which involves gathering data from the entered URL of codeforces website and saving the data into respective folders.
MOTIVE:
The main motive of this project is to help users save time while solving the problems and reduce the hassels of checking validity of their sample outputs with the expected sample outputs
"""
#import all the necessary libraries or modules
from selenium import webdriver
import chromedriver_binary # Adds chromedriver binary to path
import bs4
import requests
import os
import shutil
import filecmp
import sys
from termcolor import colored, cprint
from colorama import Fore, Back, Style
def getUrlforContest(): #function to get URL for contests
print('Enter the URL of the page(Example:https://codeforces.com/contest/1328): ', end='')
url=input()
if len(url)<30:
cprint('Invalid URL', 'red',attrs=['bold'])
sys.exit()
return url
def getUrlforSingleProblem(): #function to get URL for a problem from problemset
print('Enter the URL of the page(Example:https://codeforces.com/problemset/problem/1333/E): ', end='')
url=input()
if len(url)<43:
cprint('Invalid URL', 'red',attrs=['bold'])
sys.exit()
return url
def getInputFiles(input): #function to get sample inputs
j=0
for i in range(len(input)):
print("Getting sample input " +str(i))
x=input[i].text
#print(x)
x='\n'.join(x.split('\n')[1:])
fileName='input'+ str(j)+'.txt'
file=open(fileName,"w+")
file.write(x)
file.close()
j+=1
def getOutputFiles(output): #function to get sample outputs
k=0
for i in range(len(output)):
print("Getting sample output "+str(i))
x=output[i].text
#print(x)
x='\n'.join(x.split('\n')[1:])
fileName='output'+ str(k)+'.txt'
file=open(fileName,"w+")
file.write(x)
file.close()
k+=1
#The main menu for the project
print("---------MAIN MENU---------")
print("1.Enter 1 for live or virtual contests or to clone complete contest")
print("2.Enter 2 for a single problem from problemset")
print("Enter choice: ",end='')
choice=int(input())
if choice==1: #for complete contest
url=getUrlforContest()
tp="'"+url[len(url)-13:]
final='//a[contains(@href,'+tp+"/problem/'" + ')]'
browser = webdriver.Chrome()
browser.get(url)
elements = browser.find_elements_by_xpath(final) #get all required elements by xpath
#print(elements)
length=len(elements)
length=length//2
intialDirectory=os.getcwd()
contestFolder='Contest '+url[len(url)-4:]
os.makedirs(os.path.join(os.getcwd(),contestFolder)) #make directory for the contest
os.chdir(os.path.join(os.getcwd(),contestFolder))
for i in range(0,len(elements),2):
currentDirectory=os.getcwd()
toadd='Problem'+elements[i].text
print("Getting essentials for Problem"+elements[i].text)
res=requests.get(url+'/problem/'+elements[i].text)
os.makedirs(os.path.join(currentDirectory,toadd)) #make directory for individual problems
os.chdir(os.path.join(currentDirectory,toadd))
soup=bs4.BeautifulSoup(res.text,'html.parser') #using beautiful soup to parse HTML
input=soup.find_all('div',{'class':'input'})
output=soup.find_all('div',{'class':'output'})
j=0
#The next two shutil's are used to copy your template.cpp file and the checker script to all the problem folders
shutil.copy(os.path.join(intialDirectory,'template.cpp'),os.path.join(os.getcwd(),'soln.cpp'))
shutil.copy(os.path.join(intialDirectory,'checker.py'),os.path.join(os.getcwd(),'checker.py'))
getInputFiles(input)
getOutputFiles(output)
os.chdir(currentDirectory)
print()
browser.quit() #quitting the browser window
else: #for a single problem
url=getUrlforSingleProblem()
browser = webdriver.Chrome()
browser.get(url)
intialDirectory=os.getcwd()
problemFolder='Problem '+url[len(url)-6:len(url)-2]+url[len(url)-1:]
os.makedirs(os.path.join(os.getcwd(),problemFolder))
os.chdir(os.path.join(os.getcwd(),problemFolder))
res=requests.get(url)
soup=bs4.BeautifulSoup(res.text,'html.parser') #using beautiful soup to parse HTML
input=soup.find_all('div',{'class':'input'})
output=soup.find_all('div',{'class':'output'})
#The next two shutil's are used to copy your template.cpp file and the checker script to all the problem folders
shutil.copy(os.path.join(intialDirectory,'template.cpp'),os.path.join(os.getcwd(),'soln.cpp'))
shutil.copy(os.path.join(intialDirectory,'checker.py'),os.path.join(os.getcwd(),'checker.py'))
getInputFiles(input)
getOutputFiles(output)
browser.quit() #quitting the browser window

View File

@ -0,0 +1,6 @@
beautifulsoup4==4.8.1
chromedriver-binary==81.0.4044.69.0
colorama==0.4.3
requests==2.22.0
selenium==3.141.0
termcolor==1.1.0