我在python3中解决了一个编码问题。但我想知道,是否有更好的方法来编写此代码,以便在python3中运行得更快

2024-09-28 01:29:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的新手。最近我试着完成一个编码练习。我确实完成了编码练习,但是一些测试用例失败了,因为代码花费的时间太长,可能超过10秒。 我试着环顾四周,看看如何更有效地编写代码,但信息太多了。任何帮助或指导都将不胜感激。下面是一些与运动相关的细节

  • 杰克和吉尔正在玩游戏

    1. 游戏需要输入由1和0组成的随机整数序列 只有输入序列可以是(1,0,0,1,0)或(1,1,1,0,1,0)
    2. 杰克将有机会选择前n个数字。吉尔必须记下序列中剩余的数字
    3. 游戏的得分是这样做的-若输入值是1,那个么得分将增加1和1 输入值为0,分数将减少1

我必须构建一个以整数列表(序列)为输入的函数。列表最多可以有10^5个元素。函数应该返回Jack需要从输入序列中选择的最小数量的数字,以便他的分数比Jill更高

例如:我们将输入传递给函数[1,1,0,0,0]——Jack需要拾取的最小数字数为0

explanation: if jack picks no numbers - his score will be 0. jill has to pick all the remaining numbers. his score will be 1+1-1-1-1=-1. so function have to print 0 as output.

例如:我们将输入传递给函数[0,1,1,0,1]——Jack需要拾取的最小数字数为3

explanation: if jack picks no values then - jack's score -0 and score of Jill - -1+1+1-1+1=1.

  • 如果jack选择了一个值,那么jack的分数--1和Jill的分数- 1+1-1+1=2

  • 如果jack选择前2个值,则jack的分数为-1-1,分数为 吉尔-1-1+1=1

  • 如果jack选择前3个值,则jack的分数为-1-1+1=1,分数为 吉尔--1+1=0

    -- 正如你们所看到的,杰克的分数比吉尔高,所以函数必须输出3

下面是我尝试的两种方法

def game(s):
    #create a new list which substitutes 0 with -1 in input list so that I am sum the values oflist.
    newlist1=[-1 if p==0 else 1 for p in s]
    if sum(newlist1)<0:
        print("the minimum amount numbers jack need to pick is: ", 0)
    else:
        for l in range(1,len(s)+1):  # main loop
            jackscore=0
            jillscore=0       
            for m in range(l):   #loop to calculate jack's score
                if s[m] == 1:
                    jackscore=jackscore+1
                else:
                    jackscore=jackscore-1
            for n in range(l,len(s)):    #loop to calculate jack's score
                if s[n] == 1:
                   jillscore=jillscore+1
                else:
                   jillscore=jillscore-1
             #for each iteration we will check if jack's score is greater than Jill      
            if jackscore>jillscore: 
                break
        print("the minimum amount numbers jack need to pick is: ",l)

#main program starts here
if __name__ == '__main__':
    #y=[1,0,0,1,0]
    y = [int(x) for x in input("enter any number of 1 and 0 seperated by comma: ").split(',')]
    game(y)

第二次尝试

def game(s):
     #create a new list which substitutes 0 with -1 in input list so that I am sum the values oflist.
    newlist1=[-1 if p==0 else 1 for p in s]
    if sum(newlist1)<0:
        print("the minimum amount numbers jack need to pick is: ", 0)
    else:
        for l in range(1,len(s)+1):  # main loop     
            #create 2 new lists one for jack and other for Jill and sum values in list
            if sum(newlist1[:l])>sum(newlist1[l:]):
                break
        print("the minimum amount numbers jack need to pick is: ",l)

#main program starts here
if __name__ == '__main__':
    #y=[1,0,0,1,0]
    y = [int(x) for x in input("enter any number of 1 and 0 seperated by comma: ").split(',')]
    game(y)

有没有其他方法可以比这些方法跑得更快。如果是,请帮忙


Tags: thetoinforifmainelse分数

热门问题