George_Ogden's blog

By George_Ogden, history, 3 years ago, In English

For a while, I've been trying to test interactive problems manually and anyone's who's tried this will know that it's mundane, difficult and gives you a lot of errors. Hence, I wrote some Python code to interact with my compiled .exe files and check that they work for large or complex testcases within the time limit:

from subprocess import Popen, PIPE
import time

t = time.time()
n = 100000
# open exe file for interaction
pipe = Popen(['main.exe'],stdin=PIPE,stdout=PIPE,stderr=PIPE)
# write initial value
pipe.stdin.write(bytes(str(n)+"\n",encoding="ascii"))
pipe.stdin.flush()
q = 0
while True:
    # get input
    y = pipe.stdout.readline().strip().decode().split()
    print(y)
    if y[0] == "?":
        # process query
        q += 1
        pipe.stdin.write(bytes("" + "\n",encoding="ascii"))
        pipe.stdin.flush()
    else:
        # check answer
        print("Correct" if True else "Incorrect")
        break
# display runtime and query number
print("Time:",time.time() - t,"Queries:",q) 

It doesn't check that all formats are valid, but you can add your own logic for whatever you need. Please, feel free to use and hopefully, get more ACs in interactive problems.

  • Vote: I like it
  • +32
  • Vote: I do not like it