0

I have an exercise from a book with this code snippet:

def binomial_coeff(n, k):
    """Compute the binomial coefficient "n choose k".

    n: number of trials
    k: number of successes

    returns: int
    """
    if k == 0:
        return 1
    if n == 0:
        return 0

    res = binomial_coeff(n-1, k) + binomial_coeff(n-1, k-1)
    return res

the goal of the exercise is to rewrite the if statements as nested conditional expressions. I understand how to write a conditional expression e.g.

return 1 if k == 0

What am I missing here? By doing this nested, I can't seem to figure it out. PyCharm keeps complaining about that it the second part of the code is unreachable.

return 1 if k == 0 else return 0 if n == 0
2
  • 1
    Nested is nothing but if-elif-else conditional expressions and your doubt about Pycharm complaining, is that the error means any statement after a return is unreachable in a function Commented Sep 5, 2019 at 4:55
  • Two returns on the same line is actually invalid syntax, so IDK why you're getting a warning about unreachable code. Commented Sep 2, 2024 at 18:29

4 Answers 4

3
return 1 if k == 0 else (0 if n == 0 else binomial_coeff(n-1, k) + binomial_coeff(n-1, k-1))

but seriously: why would you want to do that? That's insanely unreadable.

Sign up to request clarification or add additional context in comments.

3 Comments

It is an exercise from a book like I wrote initially.
Yeah, I know. But if someone writes such exercises and does not clearly state that it is a bad code then perhaps you should change the book. I mean this exercise teaches you nothing useful.
I agree with you there this is not very useful. Especially because the book only talks about conditional expressions on half a page but doesn't explain how to use them nested. Hence, making this a bit confusing IHMO. But thanks for the code snippet.
0
def binomial_coeff(n, k):
    """Computes the binomial coefficient "n chose k".
    n: number of trials
    k: number of successes

    returns: int
    """
    return 1 if k == 0 else (0 if n == 0 else (binomial_coeff(n - 1, k) + binomial_coeff(n - 1, k - 1)))


bi_memo = {}


def binomial_coeff_memo(n, k):
    bi_memo[n, k] = 1 if k == 0 else (0 if n == 0 else (
        bi_memo[n, k] if (n, k) in bi_memo else (binomial_coeff_memo(n - 1, k) + binomial_coeff_memo(n - 1, k - 1))))
    return bi_memo[n, k]


if __name__ == '__main__':
    print(binomial_coeff(8, 3))
    print(binomial_coeff_memo(8, 3))

1 Comment

It is a great excercise in "Think Python 2e". Although the author doesn't explain how to use neted conditional expressions, you've already learned how to use normal conditional statements, you should try to think and test the natural way to write a nested conditional expression, or just look up to the python officlal documents.
0
def binomial_coeff(n, k):
    """Computes the binomial coefficient "n chose k".
    n: number of trials
    k: number of successes

    returns: int
    """
    return 1 if k == 0 else 0 if n == 0 else bico(n-1, k) + bico(n-1, k-1)

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
0

A little bit late, but here's my two cents:

return 1 if k == 0 else (0 if n == 0 else binomial_coeff(n - 1, k) + binomial_coeff(n - 1, k - 1))

Explanation:

Conditional expressions can be nested. If the basic syntax is:

return value1 if condition1 else value2

Then, by substituting value2 for another conditional expression (in parenthesis), we get:

return value1 if condition1 else (value2 if condition2 else value3)

which is what is needed to solve the exercise.

1 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.