Skip to content

Instantly share code, notes, and snippets.

@PanagiotisPtr
Created July 3, 2019 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PanagiotisPtr/4c0846c832f6570be539f910e22cf454 to your computer and use it in GitHub Desktop.
Save PanagiotisPtr/4c0846c832f6570be539f910e22cf454 to your computer and use it in GitHub Desktop.
codeforces parsers and C++ solution tester
import requests
from bs4 import BeautifulSoup
import sys
import os
template = """#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void solve() {
}
private:
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Solution solver;
solver.solve();
}"""
def get_testcases(url, problem):
if not os.path.exists(problem):
os.makedirs(problem)
dir_path = os.getcwd()
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
inputs = soup.find_all(class_='input')
outputs = soup.find_all(class_='output')
count = 1
for (i, o) in zip(inputs, outputs):
in_filepath = dir_path + '\\' + problem + '\\testcase_{}.in'.format(count)
with open(in_filepath, 'w') as f:
in_data = i.find(name='pre').text
f.write(in_data[1:])
out_filepath = dir_path + '\\' + problem + '\\testcase_{}.out'.format(count)
with open(out_filepath, 'w') as f:
out_data = o.find(name='pre').text
f.write(out_data[1:])
count+=1
print('Input: ', in_data)
print('Output: ', out_data)
code_filepath = dir_path + '\\' + problem + '\\solution.cpp'
with open(code_filepath, 'w') as f:
f.write(template)
page = requests.get(sys.argv[1])
soup = BeautifulSoup(page.text, 'html.parser')
table = soup.find(class_='problems')
rows = table.find_all(name='tr')
links = []
for row in rows:
if row.find(name='a'):
name = row.find(name='a').text.strip()
if len(name) > 0:
links.append([sys.argv[1] + '/problem/' + name, name])
for link in links:
name = link[1]
url = link[0]
get_testcases(url, name)
import requests
from bs4 import BeautifulSoup
import sys
import os
template = """#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void solve() {
}
private:
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Solution solver;
solver.solve();
}"""
def get_testcases(url):
dir_path = os.getcwd()
page = requests.get(url)
soup = BeautifulSoup(page.text, 'html.parser')
inputs = soup.find_all(class_='input')
outputs = soup.find_all(class_='output')
count = 1
for (i, o) in zip(inputs, outputs):
in_filepath = dir_path + '\\testcase_{}.in'.format(count)
with open(in_filepath, 'w') as f:
in_data = i.find(name='pre').text
f.write(in_data[1:])
out_filepath = dir_path + '\\testcase_{}.out'.format(count)
with open(out_filepath, 'w') as f:
out_data = o.find(name='pre').text
f.write(out_data[1:])
count+=1
print('Input: ', in_data)
print('Output: ', out_data)
code_filepath = dir_path + '\\solution.cpp'
with open(code_filepath, 'w') as f:
f.write(template)
get_testcases(sys.argv[1])
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void solve() {
}
private:
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Solution solver;
solver.solve();
}
import subprocess
import os
import glob
result = subprocess.run(['g++', 'solution.cpp', '-o', 'solution.exe'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).stdout.decode('utf-8')
if len(result):
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~ Compiler Error ~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print(result)
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
exit()
def correct(f1, f2):
s1 = open(f1, 'r').read().strip()
s2 = open(f2, 'r').read().strip()
return s1 == s2
submit = True
for file in glob.glob('*.in'):
name = file[:-3]
data_in = open(name + '.in')
data_out = open(name + '.test', 'w')
p = subprocess.Popen('solution.exe', stdin=data_in, stdout=data_out)
p.wait()
data_in.close()
data_out.close()
ans = correct(name + '.test', name + '.out')
submit = submit and ans
if ans:
print(name + ': Pass')
else:
print(name + ': Fail')
print('Input: ')
print(open(name + '.in', 'r').read().strip())
print('Correct Output: ')
print(open(name + '.out', 'r').read().strip())
print('Program Output: ')
print(open(name + '.test', 'r').read().strip())
os.remove(name + '.test')
os.remove('solution.exe')
if submit:
print('Ready to submit. Good luck!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment