The problem statement is as follows:
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.
sum67([1, 2, 2]) → 5
sum67([1, 2, 2, 6, 99, 99, 7]) → 5
sum67([1, 1, 6, 7, 2]) → 4
I'm aware of the same problem being posted here on stack overflow but I don't want to request a new solution, rather I want to know what might be the problems with my own recursive solution to the problem.
My attempt:
def sum67(nums):
def rem67(nums):
if 6 not in nums:
return nums
index6 = nums.index(6)
index7 = nums.index(7)
nums = nums[:index6] + nums[(index7 + 1):]
return rem67(nums)
return sum(rem67(nums))
The server keeps throwing maximum recursion depth exceeded error which I'm unable to rectify on the server. Any leads will be appreciated.
rem67will break if the first7comes before the first6.