Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多个测试用例Stdin(Python)_Python_Stdout_Stdin_Testcase - Fatal编程技术网

多个测试用例Stdin(Python)

多个测试用例Stdin(Python),python,stdout,stdin,testcase,Python,Stdout,Stdin,Testcase,今年facebook黑客杯的第一个问题的输入形式如下: 3 #number of test cases 4 #number of rows of test case 1 . . . x . . x x . . x x . . . x 2 #number of rows of test case 2 . . x x . . x x 3 #number of rows of test case 3 x x . . x x . . . . . x 通常,在处理codef

今年facebook黑客杯的第一个问题的输入形式如下:

 3 #number of test cases
 4 #number of rows of test case 1 
 . . . x
 . . x x
 . . x x
 . . . x
 2 #number of rows of test case 2
 . . x x
 . . x x
 3 #number of rows of test case 3 
 x x . .
 x x . .
 . . . x
通常,在处理codeforces问题或topcoder时,您不必一个接一个地输入5个测试用例,您只需为其中一个进行测试,他们就可以通过20-25个测试用例来运行它

我在试图操纵这些数据以使其可用方面做了很多努力,并且想知道如何才能做到这一点

例如,如果只是

 5
 2 3 4 5 6
我可以使用input()获得第一个数字,然后

import sys
data = []
for line in sys.stdin:
    y = [int(x) for x in line.split()]
    data.append(y)
操纵其余部分。如果我为这个问题做了类似的事情(用str替换int),我将得到一个数组,比如[3,4,data,2,data,3,data],它似乎很难操作


如何从stdin读取多个测试用例?(即使是一般性的回答也很有帮助,因为问题本身并没有那么具体)

我倾向于将其总结在生成器中。例如:

import sys

def read_data(source):
    N = int(next(source))
    for case in range(N):
        num_rows = int(next(source))
        rows = [next(source).split() for i in range(num_rows)]
        yield rows

for case in read_data(sys.stdin):
    print case
产生

dsm@notebook:~/coding$ cat source.txt | python getdata.py 
[['.', '.', '.', 'x'], ['.', '.', 'x', 'x'], ['.', '.', 'x', 'x'], ['.', '.', '.', 'x']]
[['.', '.', 'x', 'x'], ['.', '.', 'x', 'x']]
[['x', 'x', '.', '.'], ['x', 'x', '.', '.'], ['.', '.', '.', 'x']]

这样,数据读取器就不在乎源代码是stdin还是文件,或者其他什么,如果需要,您可以向其传递删除注释的内容。

我倾向于将其封装在生成器中。例如:

import sys

def read_data(source):
    N = int(next(source))
    for case in range(N):
        num_rows = int(next(source))
        rows = [next(source).split() for i in range(num_rows)]
        yield rows

for case in read_data(sys.stdin):
    print case
产生

dsm@notebook:~/coding$ cat source.txt | python getdata.py 
[['.', '.', '.', 'x'], ['.', '.', 'x', 'x'], ['.', '.', 'x', 'x'], ['.', '.', '.', 'x']]
[['.', '.', 'x', 'x'], ['.', '.', 'x', 'x']]
[['x', 'x', '.', '.'], ['x', 'x', '.', '.'], ['.', '.', '.', 'x']]
这样,数据读取器就不关心源代码是stdin还是文件,或者其他什么,如果需要,您可以向它传递一些删除注释的内容