Python 如何从接受输入更改为检查列表中的输入?

Python 如何从接受输入更改为检查列表中的输入?,python,Python,这是我的输入列表 class TestCase: def __init__(self,input,output): self.input = input self.output = output testCases = [] t1 = TestCase((1,[''],['6 6'],['0 8 9 2 7 5'],['5 4'],['5 3'],['1 5'],['0 1'],['0 2'],['2 1']), 'Case 1:

这是我的输入列表

class TestCase:
  def __init__(self,input,output):
    self.input = input
    self.output = output
    
testCases = []
t1 = TestCase((1,[''],['6 6'],['0 8 9 2 7 5'],['5 4'],['5 3'],['1 5'],['0 1'],['0 2'],['2 1']), 
                 'Case 1: 29 4')
testCases.append(t1)
这是我的密码

num = int(input())

for i in range(num):
  input()

  v, e = list(map(int, input().split()))
  values = list(map(int, input().split()))
  edges = [[] for i in range(v)]
  for j in range(e):
    f, t = list(map(int, input().split()))
    edges[f].append(t)
  
  root = 0
  total = 0
  while len(edges[root]) > 0:
    t = [(x, values[x]) for x in edges[root]]
    dst = max(t, key=lambda x:x[1])
    total += dst[1]
    root = dst[0]
  
  print("Case {}: {} {}".format(i+1, total, root))
输出是这样的

1

6 6
0 8 9 2 7 5
5 4
5 3
1 5
0 1
0 2
2 1
Case 1: 29 4
我的问题是如何将我的代码从接受用户输入更改为只从列表(t1)中读取或检查它。请帮忙。多谢各位

我还有一件事。如何在此处运行和检查结果?我尝试了很多次,但第6行出现了一个问题,即“tuple”对象不可调用。这些我该怎么办

# Test script

for i in range(len(testCases)):
  print(testCases[i].input)
  print("*** Testing program with test case no.", i)
  %timeit global output; output=solve(testCases[i].input)

  if output==testCases[i].output:
    print("Okay")
  else:
    print("Incorrect!\nExpected output:", testCases[i].output)
    print("Actual output:", output)
这里是问题链接

您可以重写
input()
函数的定义,以从TestCase类返回值

我想我明白你的输入结构是什么了。开头的
1
定义了测试用例中有多少组数据,其余的值是包含那么多项的列表。由于第一个值是
1
,因此每个辅助列表中只有一个值。它是每个列表中的第一个值,您要在第一个值之后为每次调用
input
返回该值

我猜,您显示的“输出”实际上是用户输入,除了最后一行。因此,当从数据集中读取数据而不是接受用户输入时,打印
input()
函数返回的每个值将验证发生了什么,并为您提供显示的输出

这是您的代码的扩展版本,我认为它符合您的要求。我在
TestCase
类中添加了一个方法来返回该测试用例的输入数据,一次返回一个值。我将代码的主体放在接受
TestCase
对象的
main()
函数中。在
main()。我根本不需要更改代码的主体。以下是我所做的:

# Run the main code via the supplied "input" function.  This function's name
# shadows the global function with the same name on purpose, as the code in
# this function was originally written to only accept input from the user, and
# we're demonstrating how to allow input to come from elsewhere without changing
# the original code

def solve(input):

    num = int(input())

    for i in range(num):
        input()

        v, e = list(map(int, input().split()))
        values = list(map(int, input().split()))
        edges = [[] for i in range(v)]
        for j in range(e):
            f, t = list(map(int, input().split()))
            edges[f].append(t)

        root = 0
        total = 0
        while len(edges[root]) > 0:
            t = [(x, values[x]) for x in edges[root]]
            dst = max(t, key=lambda x: x[1])
            total += dst[1]
            root = dst[0]

        result = "Case {}: {} {}".format(i + 1, total, root)
        print(result)
        return result

class TestCase:
    def __init__(self, input, output):
        self.input = input
        self.output = output

        # Additional instance variable to track what test data value to return next
        self.input_ptr = 0

    # Additional method that returns the next test data value from the test case input
    def next_input(self):
        v = t1.input[self.input_ptr]
        r = str(v) if self.input_ptr == 0 else v[0]
        self.input_ptr += 1
        print(r)
        return r

testCases = []
t1 = TestCase((1,[''],['6 6'],['0 8 9 2 7 5'],['5 4'],['5 3'],['1 5'],['0 1'],['0 2'],['2 1']),
                 'Case 1: 29 4')
testCases.append(t1)

# NOTE: The original behavior of the main code can still be obtained by passing the standard "input"
# function to it, like this:
#
# solve(input)

# Test script
for i in range(len(testCases)):
    print(testCases[i].input)
    print("*** Testing program with test case no.", i)
    output = solve(testCases[i].next_input)
    if output == testCases[i].output:
        print("Okay")
    else:
        print("Incorrect!\nExpected output:", testCases[i].output)
        print("Actual output:", output)
结果:

1

6 6
0 8 9 2 7 5
5 4
5 3
1 5
0 1
0 2
2 1
Case 1: 29 4
Okay

你为什么要这么做?这听起来是个坏主意。请链接到问题。您的代码到处都在使用
input()
,因此它希望其数据来自标准输入。相反,只需将其作为一个函数编写,该函数需要一个列表作为参数,并在函数中使用该参数?这是问题链接。非常感谢您,先生!我还有一件事。如何在此处运行和检查结果?我尝试了很多次,但第6行出现了一个问题,即“tuple”对象不可调用。这些我该怎么办范围内i的测试脚本(len(testCases)):打印(testCases[i].input)打印(“***带有测试用例号的测试程序”,i)%timeit全局输出;输出=solve(testCases[i]。输入)如果输出=testCases[i]。输出:打印(“好”)否则:打印(“不正确!\nExpected output:”,testCases[i]。输出)打印(“实际输出:”,output)您只需要
output=solve(testCases[i]。next_input)
而不是
output=solve(testCases[i]。input)
。为了不必更改现有代码的逻辑,您需要调用一个类似于
input()
函数但返回测试数据的函数。该函数是我添加到TestCase类中的方法。您需要将该方法传递到主
solve()
函数中,以便调用它,而不是标准的
input()
函数。在做了这个小改动之后,我已经更新了我的答案,加入了你的新代码。天哪,非常感谢你!你是我的上帝。我终于可以安睡了。我希望你有史以来最好的圣诞节和新年假期。再次感谢你。你是传奇!我希望您不介意我再问一次,先生。我试图添加更多的测试用例,比如t1、t2、t3、…、t10。我应该如何在下一个输入中编辑它。我建议您不要在第一个测试用例中添加更多数据。相反,只需创建更多的测试用例实例……t2、t3、t4等等。这非常简单,对吗?您的代码已经设置为在
testCases
列表上迭代。只需添加更多的TestCase对象实例即可。如果您希望一个TestCase对象中包含多组数据,那么这将有点困难。试着自己解决它。我打赌你能得到它。