nnmeduthrow
nnmeduthrow

Reputation: 37

Return Sum excluding K and following number in list using Recursion

My code is supposed to receive an integer and a list of integers. It returns the sum of the integers in the list EXCEPT, it ignores the unlucky number and the number immediately following the unlucky number. Im only trying to use recursion (NO ITERATION). So far my code works for the first 5 cases but has issues with the last 2 because they have unlucky numbers at the end. I ma not trying to completely rewrite my function only find the error in what i currently have.

def unlucky(unlucky_num, a_list):
    """
    >>> unlucky(13,[1])
    1
    >>> unlucky(13,[1,2,3,4,5,6])
    21
    >>> unlucky(13,[13,1,2,3])
    5
    >>> unlucky(13,[1,13,2,3])
    4
    >>> unlucky(13,[13, 0])
    0
    >>> unlucky(13,[13,1,2,13,2,1,13]) #13,1,13,2,13 ignored
    3
    >>> unlucky(7,[7,4,5,7,5,4,7]) #7,4,7,5,7 ignored (7 is unlucky)
    9
    """

    if a_list == []:
        return 0
    if a_list[0] == unlucky_num:
        if len(a_list) > 1:
            return unlucky(unlucky_num, a_list[2:])
        return unlucky(unlucky_num, a_list)
    return a_list[0] + unlucky(unlucky_num, a_list[1:]) 

for the last two cases I am getting the following errors (filename redacted):

Error
**********************************************************************
File "------", line 39, in sum_unlucky
Failed example:
    sum_unlucky(13,[13,1,2,13,2,1,13]) #13,1,13,2,13 ignored
Exception raised:
    Traceback (most recent call last):
      File "-------", line 138, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest sum_unlucky[5]>", line 1, in <module>
        sum_unlucky(13,[13,1,2,13,2,1,13]) #13,1,13,2,13 ignored
      File "-------", line 50, in sum_unlucky
        return sum_unlucky(unlucky_num, a_list[2:])
      File "---------", line 52, in sum_unlucky
        return a_list[0] + sum_unlucky(unlucky_num, a_list[1:])
      File "--------", line 50, in sum_unlucky
        return sum_unlucky(unlucky_num, a_list[2:])
      File "--------", line 52, in sum_unlucky
        return a_list[0] + sum_unlucky(unlucky_num, a_list[1:])
      File "-------", line 51, in sum_unlucky
        return sum_unlucky(unlucky_num, a_list)
      File "------", line 51, in sum_unlucky
        return sum_unlucky(unlucky_num, a_list)
      File "------", line 51, in sum_unlucky
        return sum_unlucky(unlucky_num, a_list)
      [Previous line repeated 986 more times]
      File "----", line 45, in sum_unlucky
        if a_list == []: 
    RecursionError: maximum recursion depth exceeded in comparison

Upvotes: 0

Views: 81

Answers (3)

wjandrea
wjandrea

Reputation: 33107

You forgot to exclude the unlucky number if len(a_list) == 1. Use a_list[1:].

def unlucky(unlucky_num, a_list):
    if a_list == []:
        return 0

    if a_list[0] == unlucky_num:
        if len(a_list) > 1:
            return unlucky(unlucky_num, a_list[2:])
        return unlucky(unlucky_num, a_list[1:])  # <- Here
    return a_list[0] + unlucky(unlucky_num, a_list[1:])

But in that case since a_list[1:] == [], you can skip the recursive call and just return 0. You could simplify that too:

def unlucky(unlucky_num, a_list):
    if a_list in ([], [unlucky_num]):
        return 0
    elif a_list[0] == unlucky_num:
        return unlucky(unlucky_num, a_list[2:])
    return a_list[0] + unlucky(unlucky_num, a_list[1:])

Upvotes: 0

Mark
Mark

Reputation: 92460

You really only need two tests, which will make it easier to avoid bugs. A test for the base case, which is an empty array and a test for the unlucky number. You don't need to test for the length after the base case because you can pass an empty array back. With that the function can be written in a way that it mirrors the requirements in very readable way:

def unlucky(unlucky_num, a_list):
    if not a_list:                             # base case
        return 0 

    head, *rest = a_list                       # rest will be [] when len(a_list) == 1

    if head == unlucky_num:
        return unlucky(unlucky_num, rest[1:])
    return head + unlucky(unlucky_num,rest)

unlucky(7,[7,4,5,7,5,4,7])
# 9

Upvotes: 1

stevemo
stevemo

Reputation: 1097

As Mark Meyer said, it never reaches a_list == []. Try:

def unlucky(unlucky_num, a_list):
    if len(a_list) == 1:
        if a_list[0] == unlucky_num:
            return 0
        else:
            return a_list[0]
    if a_list[0] == unlucky_num:
        if len(a_list) > 1:
            return unlucky(unlucky_num, a_list[2:])
        return unlucky(unlucky_num, a_list)
    return a_list[0] + unlucky(unlucky_num, a_list[1:]) 

Upvotes: 0

Related Questions