Skip to content Skip to sidebar Skip to footer

Query On Python Function That Does The Same As If Statement

I have written a solution for the below question. Q) Let's try to write a function that does the same thing as an if statement: def if_function(condition, true_result, false_result

Solution 1:

My interpretation of the question is that you have to write c(), f(), and t() such that with_if_statement() and with_if_function() return different results.

With the definitions you have given, they currently both return True, which indicates your solution is not correct.

I believe there are almost certainly multiple solutions, but here is one possible solution:

defc():
    "*** YOUR CODE HERE ***"returnTruedeft():
    "*** YOUR CODE HERE ***"ifnothasattr(f, "beencalled"): 
        return1return0deff():
    "*** YOUR CODE HERE ***"
    f.beencalled = Truereturn0print(with_if_function())
print(with_if_statement())

Here with_if_function returns 1, while with_if_statement returns 0, thus satisfying the requirement that one of the functions returns the number 1, but the other does not .

Solution 2:

What you may be missing is that you're passing the result of your functions into if_function and not the functions themselves. So this:

if_function(c(), t(), f())

...is equivalent to:

_c = c()
_t = t()
_f = f()
if_function(_c, _t, _f)

That is, your condition function, true_result function and false_result function are all called beforeif_function.

With a little extra effort, though, it's easy to make it more similar:

def delayed_call(x):
    # if x is a function, call it and return the result, otherwise return x
    return x() if hasattr(x, '__call__') else x

def if_function(condition, true_result, false_result):
    if delayed_call(condition):
        return delayed_call(true_result)
    else:
        return delayed_call(false_result)

And then if_function(c(), t(), f()) becomes if_function(c, t, f)

Solution 3:

defif_function(condition, true_result, false_result):
    """Return true_result if condition is a true value, and
    false_result otherwise.

    >>> if_function(True, 2, 3)
    2
    >>> if_function(False, 2, 3)
    3
    >>> if_function(3==2, 3+2, 3-2)
    1
    >>> if_function(3>2, 3+2, 3-2)
    5
    """if condition:
        return true_result
    else:
        return false_result


defwith_if_statement():
    """
    >>> with_if_statement()
    1
    """if c():
        return t()
    else:
        return f()

defwith_if_function():
    return if_function(c(), t(), f())

The question requires that, write 3 functions: c, t and f such that with_if_statement returns 1 and with_if_function does not return 1 (and it can do anything else)

At the beginning, the problem seems to be ridiculous since, logically, with_if_statement returns and with_if_function are same. However, if we see these two functions from an interpreter view, they are different.

The function with_if_function uses a call expression, which guarantees that all of its operand subexpressions will be evaluated before if_function is applied to the resulting arguments. Therefore, even if c returns False, the function t will be called. By contrast, with_if_statement will never call t if c returns False. (From UCB website)

defc():
    return True

deft():
    return1deff():
    '1'.sort()

Post a Comment for "Query On Python Function That Does The Same As If Statement"