2011-12-05 100 views
4

我正在以下codingbat问题:CodingBat sum67:为什么这个解决方案是错误的?

返回阵列中的数的总和,除了忽略数字区段开头的6并延伸到下一个7(每6之后,将至少有一个7)。没有号码返回0。

sum67([1, 2, 2]) → 5 
sum67([1, 2, 2, 6, 99, 99, 7]) → 5 
sum67([1, 1, 6, 7, 2]) → 4 

我的解决办法是:

def sum67(nums): 
    sum = 0 
    throwaway = 0 
    for i in range(len(nums)): 
     if throwaway == 0: 
      if nums[i] == 6: 
       throwaway = 1 
     elif throwaway == 1 and i > 0 and nums[i-1] == 7: 
      throwaway = 0 
     if throwaway == 0: 
      sum += nums[i] 
    return sum 

我完全知道这是不是最好的解决办法,但我只是好奇知道为什么这是错的。你能解释一下为什么这是错误的,在哪种情况下它会给出错误的结果?

+0

首先,代码没有正确缩进,请修复它(我没有编辑权限;))。 – hochl

+0

你知道'bool'类型吗? –

+0

'我在范围内(len(nums))'?伊克! –

回答

5

那么,你的程序有一个bug。检查下面的结果:

print sum67([1,2,5]) 
print sum67([1,2,6,5,7]) 
print sum67([1,2,6,5,7,6,7]) 

这将打印:

8 
3 
16 <-- wrong 

如果7之后立即6,您将添加6个及以后的数字。我不确定在输入中是否允许多个6 ... 7的范围,但如果是这样,您必须修正算法。除了

def sum67(nums): 
     state=0 
     s=0 
     for n in nums: 
       if state == 0: 
         if n == 6: 
           state=1 
         else: 
           s+=n 
       else: 
         if n == 7: 
           state=0 
     return s 

,如果你不需要使用索引来进行一些模糊的原因,你可以直接遍历列表(for element in list: ...)的元素:

这种简单的实现不会返回正确的数字。

+0

+1:允许多个6-7范围(有测试);对于OP的解决方案,最简单的测试用例是'sum67([6,7,6,7]) - > 0' –

+0

谢谢你,我没有任何理由的过于复杂:如果我添加了“和nums [i]!= 8在第8行,但显然你的效果要好很多 是的,我使用了一个索引,因为我想访问列表中的前一个元素,但是如你所示,这不是必需的。 – Wilco

+0

其实你可以制作我的解决方案中的'state'变量是一个'bool'('True'或'False'),但整数解决方案的优点是可以有两个以上的状态,在我看来,它更具教育意义。帮帮我! – hochl

0

这是我对该问题的解决方案。正如已经回答的那样,问题是在7之后立即发生6。我以稍微不同的方式解决了这个问题,所以我想我会发布它。

def sum67(nums): 
    total = 0 
    i=0 
    while i < len(nums): 
    if nums[i] == 6: 
     while nums[i] != 7: 
     i+=1 
     i+=1 
    if i<len(nums) and nums[i]!=6: 
     total+=nums[i] 
     i+=1 
    return total 
1
public int sum67(int[] nums) { 
int sum=0; 
    for(int i=0; i<nums.length ; i++) 



{ 
      if(nums[i]==6) 
       for(int k=i+1 ; k<nums.length ; k++) 
        {if(nums[k]==7) 
           {i=k; break;} 
        } 
      else if(nums[i]==6) 
       sum=sum+nums[i]; 
      else 
       sum=sum+nums[i]; 


    } 
    return sum; 
} 
1

下面是我供你参考的解决方案:

def sum67(nums): 
flag=False 
sum=0 

for num in nums: 
    if(num==6):     #Turn the flag on if the number is 6 
     flag=True 
     continue 
    if(num==7 and flag is True): #Turn the flag Off when 7 is seen after 6 
     flag=False 
     continue 
    if(flag is False):   #Keep on adding the nums otherwise 
     sum+=num 
return sum 
0

我的解决办法:

def sum67(nums): 
    result = 0 
    flag = True 
    for num in nums: 
     if num == 6: 
      flag = False 
     if flag: 
      result += num 
     if num == 7: 
      flag = True 
    return result 
0

我知道这是不是最好的解决办法,但认为分享到:

def detect67(nums): 
    count = nums.count(6) 
    pos6 = 0 
    pos7 = 0 
    sum1 = [] 
    for i in range (len(nums)): 
    sum1.append(nums[i]) 
    # if I do just sum1 = nums, they use SAME memory locations. 
    #print ("Count=",count) 
    for i in range (count): 
    pos6 = sum1.index(6) 
    pos7 = sum1.index(7) 
    if pos7<pos6: 
    sum1[pos7] = 0 
    pos7 = sum1.index(7) 

    del nums[pos6:pos7+1] 
    del sum1[pos6:pos7+1] 
    count = nums.count(6) 
    if count == 0: 
     return (nums) 



    return (nums) 


def sum67(nums): 

detect67(nums) 
sums=0 
if nums == []: 
    return 0 
for i in range (len(nums)): 
    sums+=nums[i] 
return sums 
相关问题