Skip to content

Instantly share code, notes, and snippets.

@pranavgade20
Last active April 20, 2022 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pranavgade20/f4aa576fcdc421f83e8aa133dd79f25f to your computer and use it in GitHub Desktop.
Save pranavgade20/f4aa576fcdc421f83e8aa133dd79f25f to your computer and use it in GitHub Desktop.
# run as `python local-judge.py your-strategy.py`
import subprocess
import sys
def main():
out = None
try:
ps = subprocess.Popen([sys.executable, sys.argv[1]], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = ps.communicate(b'''
def strategy(opponent_source: str) -> str:
return 'cooperate'
def driver():
import sys
opponent_source = ''.join(l for l in sys.stdin)
print(strategy(opponent_source))
driver()
''', timeout=6) # timeout after 6 seconds
p_res = out[-100:].strip().split(b'\n')[-1]
except subprocess.TimeoutExpired:
p_res = 'Timeout'
except Exception as ex:
print(ex)
p_res = 'Error'
if p_res in [b'defect', b'cooperate']:
print('PASSED sample testcase 1/2')
else:
print(f'FAILED sample testcase 1/2. Reason: {p_res} is not in [b\'defect\', b\'cooperate\']')
print('Program output:')
print(out)
try:
ps = subprocess.Popen([sys.executable, sys.argv[1]], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, err = ps.communicate(b'''
def strategy(opponent_source: str) -> str:
return 'defect'
def driver():
import sys
opponent_source = ''.join(l for l in sys.stdin)
print(strategy(opponent_source))
driver()
''', timeout=6) # timeout after 6 seconds
p_res = out[-100:].strip().split(b'\n')[-1]
except subprocess.TimeoutExpired:
p_res = 'Timeout'
except Exception as ex:
print(ex)
p_res = 'Error'
if p_res in [b'defect', b'cooperate']:
print('PASSED sample testcase 2/2')
else:
print(f'FAILED sample testcase 2/2. Reason: {p_res} is not in [b\'defect\', b\'cooperate\']')
print('Program output:')
print(out)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment