如何制作一个测试大量数据的高效程序

时间:2019-10-12 16:48:49

标签: python python-3.x math

我需要编写一个程序,该程序接受带有1和0的输入,并尝试找到不被1中断的最长的连续0系列。 (我设法编码了那部分)。问题出在我需要在1和0的列表中找到可以用0替换的1以使连续0的序列最长且允许g替换的时候。

我在数学上编写代码时遇到麻烦,这对于将其转换为代码很有用。我已经编写了找到最长连续0序列的程序,但是我无法编写第二部分。这是我已经做的。

# Get File Ready To Read
with open("/Users/yannkull/Desktop/marathon-sub3-attempt1.txt", "r") as f:
    current_line = f.readlines()


x = 2 
while x < 202:
    temp_list = []

    for a in current_line[x]:  # Format Line With Data
        if a != ' ':
            temp_list.append(a)
    temp_list.remove(temp_list[len(temp_list) - 1])

    k = int(current_line[x - 1])  # Recieve info line that tells us how many characters we have 

    a = 0
    sums = []
    while a < k:  # Find the longest consecutive series of zeroes in one line 

        somme = 0
        while temp_list[a] == '0':

            somme += 1
            a += 1

            if a == k:
                break

        sums.append(somme)  # Add the result to a list 
        a += 1

    print("Case #", int(x / 2 - 1), ": ", max(sums), sep='')  # Print the largest result for each case

    x += 2

我真的不知道如何实现代码中存在的问题,因此,如果有人可以给我一些建议,那将是很好的。 我对使用numpy持开放态度,只是不知道在这种情况下它将如何有用。我只是认为我的PC无法通过强力解决问题。

1 个答案:

答案 0 :(得分:1)

您可以通过假设所有替换值都应该是“连续的”来简化暴力破解:

如果您试图将两个0恰好替换为1,以便在此输入中获得最长的1序列:

  

111010100101110

您很可能会测试:

  

111X1X100101110

({X表示您尝试将0替换为1)

但不是

  

111X101001X1110

因为如果两者之间仍然有0,就没有必要替换0。

这大大减少了可能的更换可能性。