Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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
如何减少给定Python代码的执行时间?_Python_Python 3.x_While Loop - Fatal编程技术网

如何减少给定Python代码的执行时间?

如何减少给定Python代码的执行时间?,python,python-3.x,while-loop,Python,Python 3.x,While Loop,问题陈述如下: 我们有一个动物列表,其中第i个动物包含4个项目(索引、a、b和c)。最初,动物0是国王,而其他所有人排队时,动物1排在队伍的最前面,动物n排在队伍的最前面−1在后面。排在队伍前面的动物将向国王发起挑战,力量更大的动物将赢得战斗。胜利者将成为国王,而失败者则排在队伍的后面 连续三次获胜的动物将被加冕为整个动物园的统治者。每只动物的力量取决于它连续赢了多少场比赛。动物一号的实力a为0连胜,b为1连胜,c为2连胜。最初,每个人都有0连胜 对于所有动物,a>b和c>b。此外,a、b、c的

问题陈述如下:

我们有一个动物列表,其中第i个动物包含4个项目(索引、a、b和c)。最初,动物0是国王,而其他所有人排队时,动物1排在队伍的最前面,动物n排在队伍的最前面−1在后面。排在队伍前面的动物将向国王发起挑战,力量更大的动物将赢得战斗。胜利者将成为国王,而失败者则排在队伍的后面

连续三次获胜的动物将被加冕为整个动物园的统治者。每只动物的力量取决于它连续赢了多少场比赛。动物一号的实力a为0连胜,b为1连胜,c为2连胜。最初,每个人都有0连胜

对于所有动物,a>b和c>b。此外,a、b、c的值是不同的(所有3n值都是两两不同的)

我创建的函数可以工作,但函数的时间复杂性存在问题

功能:

def competition(arr):
fights = 0
wins = 1
strength_k = arr[0][1]
while wins != 4:
    king = 0
    strength_k = arr[0][wins]
    challenger = 1
    strength_c = arr[1][1]
    if strength_k > strength_c:
        wins += 1
        arr.append(arr[1])
        arr.pop(1)
    else:
        wins = 2
        arr.append(arr[0])
        arr.pop(0)
    fights += 1
    if fights >= 3 and arr[0][0] == 0:
        if arr[1][0] == 1:
            return "-1 -1"
return f"{arr[0][0]} {fights}"
,其中arr将类似于:

[[0, 5, 1, 2], [1, 10, 8, 11], [2, 9, 0, 3], [3, 7, 4, 6]]
该函数将返回新的king索引以及战斗次数

该特定arr的样本输出将为“-1-1”,因为动物将无休止地战斗而没有任何结果

注:

我认为我的终止有一个问题,当国王再次为0,挑战者为1时,我终止循环(没有结果)


有人能帮我降低同样的时间复杂度吗?

查看代码中的注释以获得简要说明:

def competition(arr):
    str_k = 1  # Start by comparing strength "a"
    fights = 0
    winners = []
    while True:
        j = 1
        while arr[0][str_k] >= arr[j][1]:
            fights += 1
            winners.append(arr[0][str_k])
            str_k += 1  # Increment strength everytime animal wins
            j += 1  # Fight the next animal
            if str_k > 3:  # 3 consecutive wins = King
                return f"King of the zoo: {arr[0][0]} with {fights} fights"
        fights += 1
        if arr[j][1] in winners:  # If the winner animal has already won before and was NOT crowned king and won AGAIN, then we are repeating the cycle thus terminate
            return "-1 -1"
        winners.append(arr[j][1])
        str_k = 2  # Start from strength "b" since it already won once
        temp = arr[0]
        arr[0] = arr.pop(j)  # Put winner animal in front
        arr.append(temp)  # Send losing animal to the back


print(competition([[0, 5, 1, 2], [1, 10, 8, 11], [2, 9, 0, 3], [3, 7, 4, 6]]))

在我看来,这个问题更适合在课堂上提问。代码评审是一个问答网站,供同行程序员进行代码评审。@Purusharth Malik如果动物输了,它的力量会重置吗?如中所述,它的力量是恢复到
a
还是保持在他输球前的最后一次力量上?@OmarAlSuwaidi在输球时,动物的力量重置为A,并被发送回队列的末尾。@Purusharth Malik您确定当动物失去力量时,不管它以前赢过什么,都会重置为A?因为在这种情况下,循环将无限运行,而给定数组没有赢家。是@OmarAlSuwaidi,对于给定数组,答案应该是“-1-1”,即没有赢家。我只是想不出我会认为一个案例无限期运行的条件。