簡體   English   中英

如何制作一個測試大量數據的高效程序

[英]How to make an efficient program that test very large amounts of data

我需要編寫一個程序,該程序接受一個零的輸入,並嘗試找到最長的連續零系列,而不會被一打斷。 (我已經設法編寫了那部分代碼)。 當我需要在 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 我只是不知道在這種情況下它會有什么用處。 我只是不認為我的電腦會通過蠻力解決這個問題。

您可以通過假設所有替換值應該是“連續的”來簡化蠻力:

如果您試圖將兩個 0 替換為 1,以便在此輸入中獲得最長的 1 序列:

111010100101110

您很可能會測試:

111X1X100101110

X表示您嘗試將 0 替換為 1)

但不是

111X101001X1110

因為如果中間還有 0,那么替換 0 是沒有意義的。

這大大減少了可能的替換可能性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM