【问题标题】:What could be causing my code to give wrong answers? [closed]什么可能导致我的代码给出错误的答案? [关闭]
【发布时间】:2020-07-20 20:59:14
【问题描述】:

我很好奇为什么我的代码提供了关于 QUESTION 的错误答案,而如果我制作自己的列表,它会给出正确的答案。 下面是我的代码 这是要测试的LIST

X = [12,4,4,2,2,3,1,1,1,1,1]
count = 0
#j is the second element
#it's like i+1
j = 1

for i in range(len(X)-1):
    if j == len(X):
        break
    if (X[i]>X[j]) == True:
        count+=1
    j += 1

print(count)

【问题讨论】:

    标签: python-3.x list list-comprehension


    【解决方案1】:

    我尝试对您的代码进行最低限度的编辑,以使其输出与您链接的 Stack Overflow 帖子相同的答案。我的解决方案如下。此外,我已经评论了错误的来源。您的解决方案之所以有效,是因为在您提供的列表中,每个大于右侧整数的整数也严格大于右侧的所有整数;您的代码计算这些大于右侧整数的整数:

    ##### YOUR SOLUTION
    
    X = #list
    count = 0
    j = 1
    for i in range(len(X)-1):
    
        # This if condition evaluates to true if you are observing the last digit.
        # "Is the last digit greater than all digits to the right?"
        # This is trivial.
        if j == len(X):
            break
    
        # Is a number greater than the number immediately to the right?
        # (This does not consider all the numbers to the right, only the one directly next to it.)
        if (X[i]>X[j]) == True:
            count+=1
    
        # j will be each index [0, len(X) - 1] EXACTLY ONCE, not the correct behavior.
        j += 1
    
    print(count)
    
    
    ##### MY SOLUTION
    
    count = 0
    j = 1
    for i in range(len(X)-1):
    
        # Set j to be the index one greater than i
        j = i + 1
    
        # While, for an integer i, there exists more digits to the right.
        while j < len(X):
    
            # If any integer after i greater than or equal to i ...
            if X[i] <= X[j]:
                break
    
            # increment j
            j += 1
    
        # If we got to the last integer without finding an integer greater than i
        # then j now points to one index greater than those in list and increment count
        if j == len(X):
            count += 1
    
    print(count)
    

    【讨论】:

      【解决方案2】:

      回答您发布的链接中存在的问题的正确方法如下 -

      X = [12,4,4,2,2,3,1,1,1,1,1]
      count = 0
      
      for i in range(len(X)-1): # This will loop till len(X) - 1
          flag = 0
          for j in range(i+1,len(X)): # For each element, check if it is greater than all elements on right 
              if X[j] >= X[i]:
                  flag = 1
                  break
          # If above condition inside for loop never passes, we can increase the count :)
          if flag == 0:
              count+=1
      
      print(count)
      

      输出:

      3
      

      上述解决方案将检查当前数字之后的每个数字。在您的解决方案中,您的逻辑不正确。您只使用下一个后续数字检查每个数字。但是你必须检查右边的每个后续数字(而不仅仅是 1)。

      还有一个O(N) 可以解决这个问题,它比这个更有效。如果您正在寻找更优化的解决方案,您应该参考您在 O(N) 解决方案问题中发布的链接。但是,上面的代码是以幼稚的方式做的正确方法。

      【讨论】:

      • @ShadowRanger 不,我不能那样做。在 for 循环中,我首先必须检查所有元素,并且只有当它们都没有通过循环条件时,才只运行外部 if。为此,使用标志是必不可少的
      • 啊哈,我理解我的错误,谢谢。那为什么它起作用了?我想是巧合。
      • @EmmanuelManoloSiame 在您的列表中,您的代码通过了三项检查 - 12 &gt; 4、4 &gt; 2 和 3 &gt; 1,但正确答案也是 3 只是巧合。在其他情况下,您的代码会产生错误的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 2021-12-25
      • 1970-01-01
      • 2016-04-13
      相关资源
      最近更新 更多