对于python练习的两个实例中的循环失败

时间:2014-01-13 19:03:14

标签: python

我是python的新手并且一直在进行在线练习,并且想知道是否有人可以解释为什么以下解决方案失败。我有两个解决方案,每个解决方案都会产生不同的故障。谢谢!

另外,为什么我需要在我的第二个代码块中分配一个变量而不是我的第一个?如果我没有分配变量,我会得到pop index out of range error

问题

返回数组中数字的总和,为空数组返回0。除了数字13是非常不吉利的,因此它不计算在13之后立即出现的数字也不计算。

sum13([1, 2, 2, 1]) → 6
sum13([1, 1]) → 2
sum13([1, 2, 2, 1, 13]) → 6

解决方案1 ​​

def sum13(nums):
    for i in nums: 
      if i == 13 and (nums.index(i) > len(nums) - 2):
        nums.pop(nums.index(i))
        continue       
      if i == 13 and (nums.index(i) < len(nums) - 1):      
        y = nums.index(i)
        nums.pop(y)
        nums.pop(y) + 1              
return sum(nums)     




sum13([13, 1, 2, 13, 2, 1, 13]) → 3               3 OK       
sum13([]) → 0                                     0 OK       
sum13([13]) → 0                                   0 OK       
sum13([13, 13]) → 0                               0 OK       
sum13([13, 0, 13]) → 0                  FAILED    13 X       
sum13([13, 1, 13]) → 0                  FAILED    13 X      
sum13([5, 7, 2]) → 14                             14 OK       
sum13([5, 13, 2]) → 5                             5 OK       
sum13([0]) → 0                                    0 OK       
sum13([13, 0]) → 0                                0 OK       
other tests                                       OK     

解决方案2

def sum13(nums):
    for i in nums: 
      if i == 13 and (nums.index(i) > len(nums) - 2):
        nums.pop(nums.index(i))
        continue       
      if i == 13 and (nums.index(i) < len(nums) - 1):      
        y = nums.index(i)
        nums.pop(y)
        nums.pop(y) + 1     
      if i == 13 and len(nums) <= 1:
        return 0
    return sum(nums)


sum13([13, 1, 2, 13, 2, 1, 13]) → 3                3 OK       
sum13([]) → 0                                      0 OK       
sum13([13]) → 0                                    0 OK       
sum13([13, 13]) → 0                                0 OK       
sum13([13, 0, 13]) → 0                             0 OK       
sum13([13, 1, 13]) → 0                             0 OK       
sum13([5, 7, 2]) → 14                             14 OK       
sum13([5, 13, 2]) → 5                      FAILED  0 X       
sum13([0]) → 0                                     0 OK       
sum13([13, 0]) → 0                                 0 OK       
other tests                                FAILED    X 

3 个答案:

答案 0 :(得分:0)

好像你试图在你的第二个if中实际执行nums.pop(y + 1),但除了那个错误之外,它无论如何都不会起作用,因为你只是从数组中弹出一个值,所以你的索引值已经改变。

答案 1 :(得分:0)

我同意其他人的意见。在迭代它时,您不希望修改列表。

这是我为您的问题编写的简单解决方案。

def sum13(nums):
    # start unlucky_index  at a big number so it can never practically happen
    # at the beginning
    unlucky_index = 0xFFFF 
    sum = 0

    for i, num in enumerate(nums):

        # if num is 13 save index of that 13
        if num == 13:
            unlucky_index = i
            continue
        # if this is the next number after the 13 index
        # then ignore
        if i == unlucky_index + 1:
            continue

        sum += num

    print "sum =",sum # debug only

    return sum

答案 2 :(得分:0)

听取评论 - 除非你知道自己在做什么,否则在迭代时不要从列表中删除对象。

我写了另一个更多Pythonic的代码。看看它,看看你是否可以某种方式使用它。

def sum13(nums):
    running_total = 0
    # Make an iterator of the provided numbers. This enables us to call
    # next on it, even inside the for loop.
    inums = iter(nums)
    for n in inums:

        # Drop this number, and drop next number in the sequence.
        if n == 13:
            try:
                next(inums)    
            # If StopIteration is raised, it means that the current item
            # was the last in the iterator - break out of the loop
            except StopIteration:
                break
            continue

        # If not 13, add the current number to the running total
        else:
            running_total += n
    return running_total