3
\$\begingroup\$

Is there a way to make my code simpler?

Given three int values, (a, b, c) return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper (def fix_teen(n)) that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code three times (i.e. "decomposition"). Define the helper below and at the same indent level as the main no_teen_sum().

no_teen_sum(1, 2, 3) → \$6\$
no_teen_sum(2, 13, 1) → \$3\$
no_teen_sum(2, 1, 14) → \$3\$

def no_teen_sum(a, b, c):
    if fix_teen(a) == 0 and fix_teen(b) == 0 and fix_teen(c) == 0:
        return 0        
    elif fix_teen(a) != 0 and fix_teen(b) != 0 and fix_teen(c) != 0:
        return a + b + c
    elif (fix_teen(a) and fix_teen(b)) != 0:
        return a + b
    elif (fix_teen(b) and fix_teen(c)) != 0:
        return b + c
    elif (fix_teen(a) and fix_teen(c)) != 0:
        return a + c
    elif fix_teen(a) != 0:
        return a
    elif fix_teen(b) != 0:
        return b
    elif fix_teen(c) != 0:
        return c


def fix_teen(n):
    if n in [13, 14, 17, 18, 19]:
        return 0
    else:
        return n
\$\endgroup\$

2 Answers 2

12
\$\begingroup\$

I may be off the mark here as I didn't test my code (no access to a Python environment), but this is the way I'd 'start' to work at it.

def no_teen_sum(a, b, c):
    return fix_teen(a) + fix_teen(b) + fix_teen(c)

def fix_teen(n):
    if n in [13, 14, 17, 18, 19]:
        return 0
    else:
        return n

Quite simple.

\$\endgroup\$
1
  • \$\begingroup\$ looks great, I wasn't able to break it so far \$\endgroup\$
    – noob81
    Commented Jul 24, 2015 at 8:52
11
\$\begingroup\$

I think you've missed the point of the exercise, which is that you should be able to make no_teen_sum() simpler, not more complicated, by defining fix_teen() as a helper function.

@insidesin has given an excellent solution. It is definitely the most straightforward way to write it, especially for beginners. I'll give another solution that uses some more advanced language features in Python.

def no_teen_sum(*numbers):
    return sum(fix_teen(n) for n in numbers)

def fix_teen(n):
    return 0 if n in (13, 14, 17, 18, 19) else n

*numbers lets the no_teen_sum() function take any number of arguments ("If the form “*identifier” is present, it is initialized to a tuple receiving any excess positional parameters"). I'd like to use the built-in sum() function, but replacing each number with the "fixed" version, using a generator expression. I've written fix_teen(), using a conditional expression.

\$\endgroup\$
2
  • \$\begingroup\$ I tend to complicate things when looking for solutions, do you have any tips on not moving towards that way of thinking? \$\endgroup\$
    – noob81
    Commented Jul 24, 2015 at 9:38
  • \$\begingroup\$ @noob81 Took me several edits to get to my final post, and even that could be improved. The key to thinking like that is to realise that nothing is ever complete or perfect. In order to start with something that's less complicated you probably need to spend a little bit of time thinking what do I really need to do? \$\endgroup\$
    – insidesin
    Commented Jul 24, 2015 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.