-
-
Save pranavgade20/f4aa576fcdc421f83e8aa133dd79f25f to your computer and use it in GitHub Desktop.
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
# 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