0

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):
    """Return true_result if condition is a true value, and false_result otherwise."""
    if condition:
        return true_result
    else:
        return false_result

This function actually does not do the same thing as an if statement in all cases. To prove this fact, write functions c, t, and f such that one of the functions returns the number 1, but the other does not:

def with_if_statement():
    if c():
        return t()
    else:
        return f()

def with_if_function():
    return if_function(c(), t(), f())

def c():
    "*** YOUR CODE HERE ***"

def t():
    "*** YOUR CODE HERE ***"

def f():
    "*** YOUR CODE HERE ***"

Solution:

>>> def c():
    return 2 < 3

>>> def t():
    return 2 < 3

>>> def f():
    return not 2 < 3

>>> print(with_if_function())
True
>>> 

My question:

Can you please confirm, if my solution is correct?

or

Do you think am yet to understand this question correctly?

12
  • What is the point of implementing such a function if you still use if in it? Commented Apr 4, 2014 at 6:59
  • @HerrActress because if is a statement and if_function is an expression. The latter can be used in places the former cannot. Commented Apr 4, 2014 at 7:02
  • @MatthewTrevor This makes sense. I guess I misinterpreted the question. Commented Apr 4, 2014 at 7:04
  • @HerrActress That's okay, I think I did too :) Commented Apr 4, 2014 at 7:06
  • @MatthewTrevor I tried writing the solution, But i did not think that this expression can be used in places): Such kind of conversion leads to an application which has only functions??? and Do you like that? Commented Apr 4, 2014 at 9:46

3 Answers 3

3

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:

def c():
    "*** YOUR CODE HERE ***"
    return True

def t():
    "*** YOUR CODE HERE ***"
    if not hasattr(f, "beencalled"): 
        return 1
    return 0

def f():
    "*** YOUR CODE HERE ***"
    f.beencalled = True
    return 0

print(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 .

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

11 Comments

Whoever left a downvote on this answer, I am requesting some explanation. The question is explicitly asking for his answer is correct or incorrect, not asking for an actual solution.
if i say >>>dir(f) i dont see 'beencalled' attribute
@merlin2011 Shouldn't the code in t and f be the other way around?
@Sham, That is because beencalled will not exist until f() has been called at least once. Instead of using an attribute, I could have used a globally scoped variable, but I was not sure if your requirements allowed for that, so I hid it inside the function instead.
@MatthewTrevor, The only requirement is that one function returns 1 and the other does not. It does not say which function should return which. When you combine the code in my answer with the code provided by the OP, you will see a 1 printed and also a 0 printed. I think if you switched it, you would probably just switch which function returned what, but I have not tested that way, so I cannot say.
|
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 before if_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)

1 Comment

I do not think he's asking how to "fix" it, although that may be the next part. He is asking for a counterexample that breaks his implementation.
1
def if_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


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

def with_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)

def c():
    return True

def t():
    return 1

def f():
    '1'.sort()

Comments

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.