#!/usr/bin/env python3 # # Testing tool for the Access Denied problem # # Usage: # # python3 testing_tool.py [-p password] # # If no password is specified, the sample is used. # # You can compile and run your solution as follows. # - You may have to replace 'python3' by just 'python'. # - On Windows, you may have to replace '/' by '\'. # # If you have a Java solution that you would run using # "java MyClass", you could invoke the testing tool with: # # python3 testing_tool.py java MyClass # # If you have a Python solution that you would run using # "python solution.py", you could invoke the testing tool with: # # python3 testing_tool.py python solution.py # # If you have a C++ solution stored in a file called "sol.cpp", # you must first compile using "g++ sol.cpp -o sol" and then # invoke the testing tool with: # # python3 testing_tool.py ./sol # # The tool is provided as-is, and you should feel free to make # whatever alterations or augmentations you like to it. # # The tool attempts to detect and report common errors, but it # is not guaranteed that a program that passes the testing tool # will be accepted. # import argparse import subprocess import sys import traceback def write(p, line): assert p.poll() is None, 'Program terminated early' print('Write: {}'.format(line), flush=True) p.stdin.write('{}\n'.format(line)) p.stdin.flush() def read(p): assert p.poll() is None, 'Program terminated early' line = p.stdout.readline().strip() assert line != '', 'Read empty line or closed output pipe. Make sure that your program started successfully.' print('Read: %s' % line, flush=True) return line def op_if()->int: return 1 def op_for()->int: return 1 def op_assign()->int: return 1 def op_comp()->int: return 3 def op_return()->int: return 1 def op_init()->int: return 1 def delay(user_pw: str, correct_pw: str)-> int: assert len(user_pw)>0 and len(user_pw)<21, 'Entered Password has wrong length (00 and len(correct_pw)<21, 'Correct Password has wrong length (0 int: common = 0 for i, j in zip(user_pw, correct_pw): if i == j: common +=1 else: return common return common parser = argparse.ArgumentParser(description='Testing tool for the Access Denied problem') parser.add_argument('-p', dest='password', metavar='password', default="Hunter2") parser.add_argument('program', nargs='+', help='Your solution') args = parser.parse_args() steps = 0 password = args.password with subprocess.Popen(" ".join(args.program), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, universal_newlines=True) as p: try: while True: guess = read(p) assert len(guess)>0 and len(guess)<21, 'Password has wrong length (0