### ldn060904's blog

By ldn060904, history, 12 days ago,

Hello peoples of codeforces, I just want to ask a question. Recently, I found debugging interactive problems' code manually kinda frustrating. Take this problem as an example. Having to answer the code's queries on my own is really time-consuming and one mistake means typing all over again. But if we could have another code that can simply read the original code's queries and answering them automatically then it would be really useful and convenient, especially when participating in a contest. If the problem was non-interactive, I would simply use files for input and output, but interactive problems require simultaneous interaction between two codes, which makes the solution of using files failed. So I am curious to know how to implement that. I would be so appreciative if someone can provide me a template in C++. Thanks for reading and have a nice day.

• +1

 » 12 days ago, # |   0 Auto comment: topic has been updated by ldn060904 (previous revision, new revision, compare).
 » 12 days ago, # |   +15 I put all query functions in lambdas. I can easily modify it to return the value from some given data. For example void solve(){ int n; cin>>n; //vector seq(n); //cin>>seq; auto query = [&](int i,int j){ cout<<"? "<>ans; //ans = (seq[i] | seq[j]); return ans; }; } Its very easy to switch between local testing and submissions. You can also use #ifdefs, if you have to change a lot. It's perhaps not the best way to test, but I think it is simple enough.
•  » » 12 days ago, # ^ |   0 I am looking for a code that can check the original code (the code I would submit). Then I could write a code to generate random tests and check the original one. For example the problem I mentioned in the blog. I want to have a code to simply generate random sequences, run the original code and interact with it, and check whether the sequence that the first code provides are the correct ones. If that is possible, I could automatically check the original one with a lot of random tests and fix the bugs if it gives an incorrect answer. I am sorry if the blog is easily to be misunderstood.
 » 12 days ago, # | ← Rev. 5 →   +4 Code vector ANSWER = {0,0,2,3}; const int LIMIT = n+2; int NUMBER_OF_INTERACTIONS = 0; #ifndef DEBUG auto interaction = [&](string operation, int i, int j){ cout << operation << ' ' << i << ' ' << j << endl; int res; cin >> res; return res; }; auto guess = [&](vector v){ cout << '!'; for(int e : v) cout << ' ' << e; cout << endl; }; #else auto interaction = [&](string operation, int i, int j){ cout << "INTERACTION: " << operation << ' ' << i << ' ' << j << endl; NUMBER_OF_INTERACTIONS++; if(NUMBER_OF_INTERACTIONS > LIMIT){ cout << "WRONG ANSWER (INTERACTION LIMIT EXCEED)" << endl; exit(0); } if(i < 1 || i > n || j < 1 || j > n){ cout << "WRONG ANSWER (WRONG INTERACTION COMMAND)" << endl; exit(0); } if(operation == "AND"){ cout << "ANSWER : " << (ANSWER[i-1] & ANSWER[j-1]) << endl; return ANSWER[i-1] & ANSWER[j-1]; } else if(operation == "XOR"){ cout << "ANSWER : " << (ANSWER[i-1] ^ ANSWER[j-1]) << endl; return ANSWER[i-1] ^ ANSWER[j-1]; } else if(operation == "OR"){ cout << "ANSWER : " << (ANSWER[i-1] | ANSWER[j-1]) << endl; return ANSWER[i-1] | ANSWER[j-1]; } else{ cout << "WRONG ANSWER (WRONG INTERACTION COMMAND)" << endl; exit(0); } }; auto guess = [&](vector v){ cout << "INTERACTION(GUESS) : "; cout << '!'; for(int e : v) cout << ' ' << e; cout << endl; bool flag = 1; if(v.size() != ANSWER.size()){ cout << "WRONG ANSWER" << endl; } else{ for(int i = 0;i < v.size();++i) flag &= v[i] == ANSWER[i]; if(flag){ cout << "CORRECT ANSWER" << endl; } else{ cout << "WRONG ANSWER" << endl; } } }; #endif For this problem, you can write some code like this to check your code