I’ve been trying to use Python for a few sample programming competition questions, but I’ve been stumped on file reading.
I’m reading from stdin, the first line is the number of test cases that follow, each subsequent line contains two integers that I need to process. E.g.
3
4 -10
0 5
6 20
2
0 -1
20 10
etc...
I’ve found a C++ solution that looks like this:
int main()
{
int runs,a,b ;
cin >> runs ;
while(runs--)
{
cin >> a >> b ;
long long ret = solve(a,b) ;
cout << ret << endl ;
}
return 0 ;
}
The closest I’ve come up with in Python is:
t = int(raw_input())
answer = 0
while t :
n, m = map(int, raw_input().split())
answer = solve(n,m)
print answer
I’ve seen similar questions on Stack Overflow but I’m still having a tricky time wrapping my head around the Python way to do this.
If you don’t want to use raw_input you can use fileinput instead:
or with sys.stdin