2

The challenge is from codingbat - Sum the elements of the array except for all values between 6 and 7, not counting the 6 or the 7 either.

My strategy was create a new array "valid" and append valid elements of the array nums to the "valid" array, and then sum the valid array.

Because I need the index to check for a 7 once a 6 is detected, my plan was to enumerate the array, and when the value 6 is found, to loop through the remainder of the array until a 7 is found, not appending the values 6-7 to the array "valid" in the process.

I ran my code through the MIT Python tutor to see where it was going wrong, and when I increment the index after i+1 != 7 (with the intention of checking to see if the next value in the array is a 7) it increments the index (it finishes working with that index of the array and moves on to the next one, rather than sticking in the code for the 6 and continuing to loop through and check for a 7).

Appreciate any help with this, especially a solution that sticks to the original strategy but implements it correctly in the code.

def sum67(nums):
  valid = []
  for i,c in enumerate(nums):
    if c == 6:
    #when a 6 comes up
      if  nums[i+1] == 7:
      #if the next number is a 7
        i+=1
        break
        #increment and break
      else:
        i+=1
        #increment
    else:
      valid.append(c)
  return sum(valid)
        
sum67([1, 2, 2, 6, 99, 99, 7])

Edit 1: The desired output for

sum67([1, 2, 2, 6, 99, 99, 7])

is 5. (Because 1 + 2 + 2 = 5, and the 6, 99, 99, and 7 are excluded because they are or are between a 6 and a 7)

rabunc
  • 67
  • 5

3 Answers3

2

You could do this to avoid creating an extra array:

def sum67(nums):
    isWithin67 = False
    totalSum = 0
    for n in nums:
        if n == 6:
            isWithin67 = True
            continue

        if n == 7:
            isWithin67 = False
            continue

        if isWithin67:
            continue

        totalSum += n

    return totalSum
lnogueir
  • 1,859
  • 2
  • 10
  • 21
  • Thank you Inogueir, would you say the loop within a loop approach isn't workable for this challenge? – rabunc Sep 07 '21 at 16:15
  • 1
    @rabunc Np at all. In my opinion, if you have the option of not nesting loops, I would always take that route unless the code is much much uglier, which is not the case here. However, you could certainly find a nested loop approach for this problem. – lnogueir Sep 07 '21 at 16:24
2

Using an iterator, and a membership check to search for and skip to after the 7:

def sum67(nums):
    it = iter(nums)
    return sum(x for x in it if x != 6 or 7 not in it)

This goes over each number, and usually (if it's not a 6) includes it in the sum. If it is a 6, then search for the next 7 and don't include any of that in the sum (and then the next iteration continues after the 7).

Edit: Ermahgerd... CodingBat ridiculously somehow deletes iter (says 'iter' is not defined) even though it's one of the most fundamental builtin functions in Python. Oh well, we can use a genexp instead, that gets accepted there:

def sum67(nums):
    it = (x for x in nums)
    return sum(x for x in it if x != 6 or 7 not in it)
no comment
  • 6,381
  • 4
  • 12
  • 30
1

You can achieve this simply by flagging whether the list is currently between 6 and 7 or not:

def sum67(nums):
    valid = []
    include = True
    for c in nums:
        if c == 6:
            include = False
        if include:
            valid.append(c)
        if c == 7:
            include = True
    return sum(valid)

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

Output as requested

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Thank you quamrana, I did see the flagging solution when I researched answers to this, this is how I will do it going forward. I guess I'm posting here because I'm curious to see if my approach is lost or if there's a viable way to implement the loop within the loop approach. – rabunc Sep 07 '21 at 16:09