【问题标题】:Summing numbers in a list except those between 6 and 7将列表中的数字相加,但 6 到 7 之间的数字除外
【发布时间】:2017-10-28 05:59:25
【问题描述】:

我正在尝试编写一个函数,它接受一个列表并对列表中的所有数字求和,除了它忽略列表中以列表开头并扩展到 7 的部分,但在 7 之后继续求和。这里是我的代码:

def sum67(nums):
   i = 0
   sum = 0
   while i < len(nums):
      k = 0
      if nums[i] != 0:
         sum += nums[i]
         i += 1
      if nums[i] == 6:
         for j in range(i + 1, len(nums)):
            if nums[j] != 7:
               k += 1
            if nums[j] == 7:
               k += 2
               i += k

测试用例显示 6 和直到 7 的数字被忽略,而其他数字被添加到总和中,并且 7 之后的数字也被添加到总和中(如预期的那样),但由于某种原因,任何 7在第一个 7 之后 6 没有相加 - 这不是我想要的,我不确定为什么会这样。有什么建议吗?

测试用例结果:

[1, 2, 2 Expected: 5. My result: 5 (OK)

[1, 2, 2, 6, 99, 99, 7] Expected: 5. My result: 5 (OK)  
[1, 1, 6, 7, 2] Expected: 4 My result: 4 (Chill)    
[1, 6, 2, 2, 7, 1, 6, 99, 99, 7] Expected: 2    My result: 1 (Not chill)    
[1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7] Expected: 2 My result: 1    (Not chill) 
[2, 7, 6, 2, 6, 7, 2, 7] Expected: 18 My result: 9 (Not chill)

`

【问题讨论】:

    标签: python indices


    【解决方案1】:
    def sum67(nums):
        # flag to know if we are summing
        active = True
        tot = 0
        for n in nums:
            # if we hit a 6 -> deactivate summing
            if n == 6:
                 active = False
            if active:
                 tot += n
            # if we hit a seven -> reactivate summing
            if n == 7 and not active: 
                 active = True
        return tot
    

    【讨论】:

    • 正确的测试是n == 6 而不是n is 6。这是一个适用于小整数的实现依赖细节。
    【解决方案2】:

    发布的代码已完全损坏。 例如对于没有任何 6 的列表, 当最后一个元素达到nums[i] == 6 条件时,i 将超出列表范围。

    您需要完全重新考虑循环内的条件。 这是一种可行的方法。 如果当前数字是 6, 然后跳过直到你看到一个 7,而不增加总和。 否则加到总和中。 在执行这两个动作中的任何一个(跳过数字或添加到总和)之后, 递增i

    def sum67(nums):
        i = 0
        total = 0
        while i < len(nums):
            if nums[i] == 6:
                for i in range(i + 1, len(nums)):
                    if nums[i] == 7:
                        break
            else:
                total += nums[i]
    
            i += 1
    
        return total
    

    【讨论】:

      【解决方案3】:

      这是学习新 Python 技术的中间替代方法:

      import itertools as it
      
      
      def span_sum(iterable, start=6, end=7):
          """Return the sum of values found between start and end integers."""
          iterable = iter(iterable)
          flag = [True]
          result = []
      
          while flag:
              result.extend(list(it.takewhile(lambda x: x!=start, iterable)))
              flag = list(it.dropwhile(lambda x: x!=end, iterable))
              iterable = iter(flag)
              next(iterable, [])
          return sum(result)
      
      # Tests
      f = span_sum
      assert f([1, 2, 2]) == 5
      assert f([1, 2, 2, 6, 99, 99, 7] ) == 5
      assert f([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) == 2
      assert f([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) == 2
      assert f([2, 7, 6, 2, 6, 7, 2, 7]) == 18
      

      原则上,此函数过滤输入,将满足您条件的值收集到result 中并丢弃其余值,然后返回总和。特别是您可以观察以下技术:

      【讨论】:

        猜你喜欢
        • 2021-08-12
        • 2012-07-29
        • 1970-01-01
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 2015-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多