-
-
Save zodman/d8d3ede4b1f1e050caaf890424d04ee8 to your computer and use it in GitHub Desktop.
A python script to run hackerrank answers on your local. Just run it in the sample test cases folder that you downloaded from hackerrank.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import glob | |
import subprocess | |
from os.path import exists | |
from sys import exit | |
INPUT_FOLDER = 'input/' | |
OUTPUT_FOLDER = 'output/' | |
SOLUTION_FILE = 'solution.py' | |
WRONG_PLACE_TO_RUN_MSG = \ | |
'You have to run this program under, downloaded inputs and outputs.' | |
REQUIRED_FOLDERS = [INPUT_FOLDER, OUTPUT_FOLDER] | |
NUMBER_FINDER = re.compile(r'\d+') | |
def get_output_filename(input_filename): | |
return 'output/output%s.txt' % NUMBER_FINDER.findall(input_filename)[0] | |
wrong_place_to_run = False | |
for folder in REQUIRED_FOLDERS: | |
if not exists(folder): | |
print 'Folder \'%s\' does not exists. ' % folder | |
wrong_place_to_run = True | |
if wrong_place_to_run: | |
print WRONG_PLACE_TO_RUN_MSG | |
exit(1) | |
if not exists(SOLUTION_FILE): | |
print 'Solution must be inside solution.py, put your solution to there ' \ | |
'and run this command again.' | |
exit(1) | |
input_files = glob.glob('input/*.txt') | |
for input_file in input_files: | |
output_file = get_output_filename(input_file) | |
cmd = 'python solution.py' | |
process = subprocess.Popen( | |
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
stdin=subprocess.PIPE, shell=True) | |
with open(input_file) as f: | |
input_data = f.read() | |
with open(output_file) as f: | |
output_data = f.read() | |
out, err = process.communicate(input_data) | |
if err: | |
print "Error happened when executing your solution:" | |
print "--------------------------------------------" | |
print err | |
exit(1) | |
out = out[:-1] | |
if out == output_data: | |
print '%s: SUCCESS' % input_file | |
else: | |
print '%s: FAIL' % input_file | |
print "-------OUTPUT------" | |
print out% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# to use with a python3 tested on windows | |
import subprocess | |
import sys | |
cmd = [ | |
'python', | |
sys.argv[1] | |
] | |
process = subprocess.Popen( | |
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | |
stdin=subprocess.PIPE, shell=True) | |
input = open(sys.argv[2], "rb").read() | |
out_file = open(sys.argv[3], "rb").read() | |
out, err = process.communicate(input) | |
if err: | |
print(err.decode("utf-8")) | |
out = out[:-1] | |
if out == out_file: | |
print("WIN") | |
print(out.decode("utf-8")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment