CodingBat sum67: why is this solution wrong?

I am working on the following coding problem:

It returns the sum of the numbers in the array, with the exception of ignoring the sections of numbers beginning with 6 and continuing to the next 7 (at least one 7 will follow each 6). Return 0 for numbers.

sum67([1, 2, 2]) β†’ 5 sum67([1, 2, 2, 6, 99, 99, 7]) β†’ 5 sum67([1, 1, 6, 7, 2]) β†’ 4 

My decision:

 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 

I fully know that this is not the best solution, but I'm just curious to know why this is wrong. Could you explain to me why this is wrong, and in which specific case does it give the wrong result?

+5
source share
18 answers

There is an error in your program. Check the results:

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

This will print:

 8 3 16 <-- wrong 

If 7 immediately follows 6, you add 6 and all of the following numbers. I'm not sure that more than one range of 6 ... 7 is allowed at the input, but if so, you should fix your algorithm.

This simple implementation returns the correct numbers:

 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 

In addition, if you do not need to use the index for some unclear reasons, you can directly iterate over the list items ( for element in list: ... ).

+5
source

Below is my solution for your reference:

 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 
+3
source

Here is my solution to this problem. As already mentioned, the problem is when 6 occurs immediately after the 7th. I decided it a little differently, so I decided to publish it.

 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
source
 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
source

My decision:

 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 
+1
source

This is my @TheNeophyte code editing. I liked this solution, because since I probably approach the problem. The only editing really was

 if i<len(nums) and nums[i]!=6: 

and just put the expression elif without saying

 i<len(nums) 

since it is not needed again after the first while loop.

My edit below VVV

 def sum67(anArray): arrTotal = 0 n =0 while n < len(anArray): if anArray[n] == 6: while anArray[n] != 7: n+=1 n+=1 elif anArray[n] != 6: arrTotal += anArray[n] n+=1 return arrTotal 
+1
source

Just add if the last item is 7, you don't need to iterate over each item. The question says that "ignore sections of numbers starting with a 6 and go up to the next 7 (at least one 7 will follow each 6)." Once you find 6, you can simply check if the last element is 7, and if it is so simple to break free and return the amount; otherwise, you can continue to view the remaining items. This is useful when dealing with a large number of elements in an array. ex. [1, 2, 2, 6, 99, 99, 2, 99, 99, 2, 99, 99, 2, 99, 99, 7]

My decision:

 def sum67(arr): sum = 0 tmp = 0 for n in arr: if n == 6: tmp = 6 continue if tmp == 6: if n == 7: tmp = 0 continue elif arr[-1] == 7: break else: continue sum += n return sum 
+1
source

My solution for this is I know this is quite readable, but not the best code:

 def sum67(nums): offcount = False sum = 0 for i in range(len(nums)): if offcount == False: if nums[i] == 6: offcount = True else: sum = sum + nums[i] else: if nums[i] == 7 : offcount = False return sum 
+1
source

Hope this helps :)

 def sum67(nums): result = 0 inside = False for i in range(len(nums)): if not inside and nums[i] != 6: result += nums[i] if nums[i] == 6: inside = True if nums[i] == 7: inside = False return result 
+1
source
 def sum67(nums): sum = 0 sumbrake = False for i in nums: if i != 6 and sumbrake == False: sum += i else: sumbrake = True if i == 7: sumbrake= False return sum 
+1
source

I know that this is not the best solution, but we decided to share:

 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 
0
source

here is my 6 liner answer. Hope this helps

 def summer_67(arr): s = 0 for x in arr: if x not in range(6,7+1): s = s + x return s 
0
source

Well, you can use nested loops.

 def sum67(nums): s=0 flag=1 for n in nums: while flag: if n!=6: s+=n break else: flag=0 while not flag: if n!=9: break else: flag=1 return s 
0
source
 def sum67(nums): while nums.count(6) >= 1: list = (nums[0:nums.index(6)]) + (nums[nums.index(7)+1:len(nums)]) nums = list return sum(nums) 
0
source
 def sum67(arr): for n in range (0, len(arr)): if arr[n] == 6: Sum1 = sum(arr[0:n]) print ("Before 6 Sum Numbers:", Sum1) for k in range (0, len(arr)): if arr[k] ==7: Sum2 = sum(arr[(k)+1:]) print ("After 7 Sum Numbers:", Sum2) if 6 and 7 in arr: print("Last Result") return (Sum1+Sum2) else: print("Last Result") return sum(arr) 
0
source

Simplified code without while and for loops:

  def summer_69(arr): pos6 = 0; pos9 = 0; newArr = []; if 6 in arr: pos6 = arr.index(6); if 9 in arr: pos9 = arr.index(9); if 6 in arr and 9 in arr : newArr = arr[0:pos6] + arr[pos9+1:len(arr)]; else: newArr = arr; return sum(newArr); pass 
0
source

Maybe a more Pythonian approach?

 def sum67(arr): if (6 in arr): i1 = arr.index(6) i2 = arr[i1:].index(7) res = sum(arr[0:i1]+arr[i1+i2+1:]) else: res = sum(arr) return res 
0
source

Here is my 13-line answer that uses list index positions to exclude values ​​enclosed in each pair [6..7].

Technique : return the sum of array elements after subtracting (the sum) of excluded elements.

 def sum67(nums): idx_list=[] for i in range(len(nums)): if nums[i]==6: c_6 = i # index pos of 6 j = c_6+1 # counter to find index position of 7 while nums[j]!=7: # stop search when 7 spotted in list j+=1 c_7 = j # index pos of 7 idx_list += list(range(c_6,c_7+1)) # list of index position(s) of elements to be excluded idx_main = list(set(idx_list)) # set() removes repeated index positions captured while looping del_val = [nums[i] for i in idx_main] return sum(nums)-sum(del_val) 
0
source

Source: https://habr.com/ru/post/1384690/


All Articles