2014-01-13 41 views
1

我是python的新手,一直在線練習,並想知道是否有人可以解釋爲什麼下面的解決方案失敗。我有兩種解決方案,每種都會產生不同的失敗,如輸出中所示。謝謝!!For循環在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 

SOLUTION 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 
+4

不要在迭代列表時從列表中刪除項目。 – geoffspear

+1

將'print nums'添加到循環體的開頭,它應該清楚發生了什麼。 – Daenyth

+0

+1用於向我們展示您嘗試過的內容以及輸入/輸出示例。 – jramirez

回答

0

好像你正在嘗試做的其實nums.pop(Y + 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 
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