Last active
July 7, 2017 23:46
-
-
Save fritogotlayed/e638ed7d4fdd69a1fc6a7fd176d8f84f to your computer and use it in GitHub Desktop.
Extended UVA Judge Scaffold and usage
This file contains 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
D:\GitSource\programming-challenges>python ./10189.py --interactive=True | |
Please enter your program inputs. When finished just press enter again on your keyboard effectively entering a blank line. | |
4 4 | |
*... | |
.... | |
.*.. | |
.... | |
0 0 | |
Field #1: | |
*100 | |
2210 | |
1*10 | |
1110 | |
D:\GitSource\programming-challenges>python ./10189.py --submit --submitProblem 10189 | |
{u'code': u'AC', u'message': u'Accepted'} |
This file contains 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
from __future__ import print_function | |
import argparse | |
import sys | |
import os | |
import json | |
import pprint | |
arg_parser = None | |
def main(): | |
lines = get_input_lines() | |
# TODO(You): Replace all the lines below this comment in the main method. | |
print(lines) | |
### | |
# Ignore everything below this block comment as it is all part of the | |
# scaffold to ease debugging and submit to the judge. | |
### | |
def build_args_parse(): | |
parser = argparse.ArgumentParser(description='Extended UVa Judge') | |
parser.add_argument( | |
'--interactive', action='store', default='false', type=str_to_bool, | |
nargs='?', const='true', | |
help='Run the application in interactive mode.' | |
) | |
parser.add_argument( | |
'--submit', action='store', default='false', type=str_to_bool, | |
nargs='?', const='true', | |
help='Submit this application to the judge server.' | |
) | |
parser.add_argument( | |
'--endpoint', action='store', type=str, | |
default='http://fritogotlayed.hopto.org:8080', | |
help='Specify the location to submit the solution to.' | |
) | |
parser.add_argument( | |
'--submitDebug', action='store', default='false', type=str_to_bool, | |
nargs='?', const='true', | |
help='Submit this application to the judge server.' | |
) | |
parser.add_argument( | |
'--submitProblem', action='store', default='0', type=str, | |
help='Submit this application to the judge server.' | |
) | |
parser.add_argument( | |
'--submitLanguage', action='store', default='py2', type=str, | |
help='Submit this application to the judge server.' | |
) | |
parser.add_argument( | |
'--availableLanguages', action='store', default='false', | |
type=str_to_bool, nargs='?', const='true', | |
help='Query the judge server for the supported languages.' | |
) | |
parser.add_argument( | |
'--availableProblems', action='store', default='false', | |
type=str_to_bool, nargs='?', const='true', | |
help='Query the judge server for the supported problems.' | |
) | |
return parser.parse_args() | |
def str_to_bool(v): | |
if v.lower() in ('yes', 'true', 't', 'y', '1'): | |
return True | |
elif v.lower() in ('no', 'false', 'f', 'n', '0'): | |
return False | |
else: | |
raise argparse.ArgumentTypeError('Boolean value expected.') | |
def get_input_lines(): | |
global arg_parser | |
if arg_parser is None: | |
arg_parser = build_args_parse() | |
if arg_parser.interactive: | |
inputs = [] | |
print('Please enter your program inputs. When finished just press ' | |
'enter again on your keyboard effectively entering a blank line.' | |
) | |
while True: | |
line = sys.stdin.readline() | |
# standardize and remove new lines | |
line = line.replace(os.linesep, '\n').replace('\n', '') | |
if line != '': | |
inputs.append(line) | |
else: | |
break | |
else: | |
input_str = sys.stdin.read() | |
inputs = input_str.replace(os.linesep, '\n').split('\n') | |
return inputs | |
def submit_to_judge(): | |
try: | |
# noinspection PyUnresolvedReferences | |
import requests | |
except ImportError: | |
print('Library "requests" is not installed. Submission is not ' | |
'currently available. Please use "pip install requests" before ' | |
'attempting to submit again.') | |
return | |
if arg_parser.submitDebug: | |
params = {'debug': 'true'} | |
else: | |
params = None | |
full_url = '{host}/api/v1/problem/{problem}/{lang}/test'.format( | |
host=arg_parser.endpoint, | |
problem=arg_parser.submitProblem, | |
lang=arg_parser.submitLanguage) | |
data = open(__file__, 'rb') | |
file_name = '%s.py' % arg_parser.submitProblem | |
resp = requests.post(full_url, | |
files={file_name: data}, | |
params=params) | |
pprint.pprint(json.loads(resp.text)) | |
def query_available_languages(): | |
try: | |
# noinspection PyUnresolvedReferences | |
import requests | |
except ImportError: | |
print('Library "requests" is not installed. Submission is not ' | |
'currently available. Please use "pip install requests" before ' | |
'attempting to submit again.') | |
return | |
full_url = '{host}/api/v1/languages'.format( | |
host=arg_parser.endpoint) | |
resp = requests.get(full_url) | |
pprint.pprint(json.loads(resp.text)) | |
def query_available_problems(): | |
try: | |
# noinspection PyUnresolvedReferences | |
import requests | |
except ImportError: | |
print('Library "requests" is not installed. Submission is not ' | |
'currently available. Please use "pip install requests" before ' | |
'attempting to submit again.') | |
return | |
full_url = '{host}/api/v1/problems'.format( | |
host=arg_parser.endpoint) | |
resp = requests.get(full_url) | |
pprint.pprint(json.loads(resp.text)) | |
def eprint(*args, **kwargs): | |
# get rid of a file override if specified. | |
kwargs.pop('file', None) | |
print(*args, file=sys.stderr, **kwargs) | |
if __name__ == '__main__': | |
arg_parser = build_args_parse() | |
if arg_parser.availableLanguages: | |
query_available_languages() | |
elif arg_parser.availableProblems: | |
query_available_problems() | |
elif arg_parser.submit: | |
submit_to_judge() | |
else: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment