-1

Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.

Kindly Help me with the solution. For other input values i am getting correct output value.. and also suggest me a short way to write the code.. It seems bit too long

def sum67(nums):
  count=0
  i=0
  switch = 0
  if len(nums) == 0:
    count=0
  else:
    while i < len(nums):
      if nums[i] != 6 and switch == 0 and nums[i] != 7:
        count += nums[i]
        i+=1
        #print("incremented")
        continue
      if nums[i] == 6 and switch == 0:
        switch = 1
        #print("switch ON")
        i+=1
      if nums[i] == 6 and switch == 1:
        i+=1
      if nums[i]==7 and switch==0 :
        count+=nums[i]
        #print("again 7")
        i+=1
      if switch == 1 and nums[i] == 7:
        switch = 0
        #print("switch OFF")
        i+=1
      else:
        i+=1
  #print(count)
  return count

OUTPUT : Input 1 : sum67([2, 7, 6, 2, 6, 7, 2, 7]) expected :18
output I got :20

Input 2 : sum67([2, 7, 6, 2, 6, 2, 7]) expected : 9 Output I got : 11

Nithilan
  • 29
  • 1
  • 7

10 Answers10

4

Try this:

def sum67(nums):
  result = 0
  startadding = True
  for val in nums:
    if val == 6:
      startadding = False
    if startadding:
      result +=val
    if val == 7:
      startadding = True
  return result
print(sum67([2, 7, 6, 2, 6, 7, 2, 7]))
JGFMK
  • 8,425
  • 4
  • 58
  • 92
3

This was my solution, I'm pretty new to programming, not sure if this is the correct way to do it but it works

def sum67(nums):
  index = total = 0
  while index < len(nums):
    if nums[index] == 6:
      while nums[index] != 7:
        index += 1
      index += 1
    else:
      total += nums[index]
      index += 1
  return total
KWarn
  • 31
  • 1
3

Here is a solution

def sum67(nums):
    total_sum = 0
    skip_flag = False

    for number in nums:
        if number == 6:
            skip_flag = True # prepare to skip
            continue # skip this iteration
        if number == 7 and skip_flag == True:
            skip_flag = False # prepare to sum
            continue # skip this iteration
        if skip_flag == False:
            total_sum += number

    return total_sum

testing:

>>>sum67([2, 7, 6, 2, 6, 7, 2, 7]) # -> 18

>>>sum67([2, 7, 6, 2, 6, 2, 7])) # -> 9

Kais
  • 56
  • 3
2

Here is the simplest solution:

def sum67(nums):
  while 6 in nums: 
    del nums[nums.index(6):nums.index(7,nums.index(6))+1]
  return sum(nums)
macroT
  • 21
  • 2
1

Here's a far simpler solution:

def sum67(nums):
    my_sum = 0
    do_sum = True
    for num in nums:
        if num == 6:
            # stop summing
            do_sum = False
        elif num == 7:
            # start summing
            do_sum = True
            # if we were not summing then this 7 will be added to the sum, so subtract it now to keep the sum correct
            if not do_sum:
                my_sum -= 7


        if do_sum:
            my_sum += num
    return my_sum


print(sum67([2, 7, 6, 2, 6, 7, 2, 7])) # 18
print(sum67([2, 7, 6, 2, 6, 2, 7])) # 9
rdas
  • 20,604
  • 6
  • 33
  • 46
0

List-2 > sum67(python3)

def sum67(nums):
  s = sum(nums)
  l=[0]
  for i in range(len(nums)):
    if nums[i]==6 and nums[l[-1]]!=6:
      l.append(i)
    if nums[i]==7 and nums[l[-1]]==6:
      l.append(i)
      s-=sum(nums[l[-2]:l[-1]+1])
      
      
  return s
0
def sum67(nums):
    soma = 0
    if (len(nums)>0):
        i =0
        while i<len(nums):
            if (nums[i] == 6):
                while (nums[i] != 7):
                    i +=1                
            else : soma += nums[i]
            i +=1
        return soma
                
    else : return 0
Hugo Resende
  • 1
  • 1
  • 1
0
def except6_7(nums):
    if nums.count(6) == 1 and nums.count(7) == 1:
        return sum(nums)-sum(nums[nums.index(6):nums.index(7)+1])
    return sum(nums)
print(except6_7([1, 2, 2, 6, 99, 99, 7]))

or

def except6_7(nums):
      while 6 and 7 in nums: ## YOU MUST ADD "and 7" ##
            del nums[nums.index(6):nums.index(7,nums.index(6))+1]
      return sum(nums)
print(except6_7([1, 2, 2, 6, 99, 99]))
ogk
  • 3
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 02 '22 at 06:51
-1
def sum67(nums):
    my_sum = 0
do_sum = True
for num in nums:
    if num == 6:
        # stop summing
        do_sum = False
    elif num == 7 and do_sum == False:
        # start summing
        do_sum = True
        my_sum -= 7

    if do_sum:
        my_sum += num
return my_sum

print(sum67([2, 7, 4, 3, 6, 67, 45, 7, 2])) # gives output of 18
print(sum67([1, 2, 2, 6, 99, 99, 7]) # gives output 5 (both correct)

# I didn't have enough contributor points to reply to the guy who had an error in his and this is a correction of that
  • 1
    Please add an accurate description of your code instead of just pasing your code, make sure that, from next time, you provide some details and some context so that people can help you better! – XtremeDevX Jul 17 '20 at 17:00
-1
def sum67(nums):
    sum_= sum(nums)
    six = [i for i, x in enumerate(nums) if x == 6]
    seven = [i for i, x in enumerate(nums) if x == 7]
    seven = [x for x in seven if x>six[0]]
    tmp = []
    if six:
        if len(six)>len(seven):
            six = six[::2]
            for i in range(len(six)):
                sum_-=sum(nums[six[i]:seven[i]+1])
            return sum_
        elif len(six)<len(seven):
            seven = seven[:len(six)]   
            for i in range(len(seven)):
                sum_-=sum(nums[six[i]:seven[i]+1])
            return sum_
        else:    
            for i in range(len(six)-1):
                if six[i+1]<seven[i]:
                    tmp.append(six[i+1])    
            if tmp:
                for i in range(len(tmp)):
                    six.remove(tmp[i])
                for i in range(len(six)):
                    sum_-=sum(nums[six[i]:seven[i]+1])
                return sum_
            else:   
                for i in range(len(seven)):
                    sum_-=sum(nums[six[i]:seven[i]+1])
                return sum_
    else:
        return sum(nums)
TKir
  • 11
  • 2