30
5
Implement a function diff that takes as input three integers x, y, and z. It should return whether subtracting one of these numbers from another gives the third.
Test cases:
diff(5, 3, 2) yields True because 5 - 3 = 2
diff(2, 3, 5) yields True because 5 - 3 = 2
diff(2, 5, 3) yields True because 5 - 3 = 2
diff(-2, 3, 5) yields True because 3 - 5 is -2
diff(-5, -3, -2) # -5 - -2 is -3
diff(2, 3, -5) yields False
diff(10, 6, 4) yields True because 10 - 6 = 4
diff(10, 6, 3) yields False
You don't have to name the function, you may implement default input methods the examples above are not a strict guideline.
5This is a reasonable challenge, but there's no need to restrict it to Python or functions. In general, such restrictions are frowned upon because they limit participation. Also, you should include some test cases. – xnor – 2016-02-07T09:37:26.143
Hey I fixed it a little. Hope this suffices! – Mir – 2016-02-07T09:45:26.737
2
Looks better! I still strongly recommend allowing the default input methods, in particular programs, because some languages don't have functions. And, allowing functions to have another name or no name.
– xnor – 2016-02-07T09:48:15.623The first and last paragraphs are now conflicting, so just to double check - do we have to write a function or are full programs okay? – Sp3000 – 2016-02-07T10:03:27.580
full programs are fine, I want to impose as few restrictions as possible except that the default input methods are followed. ef the python3 examples is neat! – Mir – 2016-02-07T10:18:58.117
Hmm k... just another question sorry - does input have to be
– Sp3000 – 2016-02-07T10:21:19.007True
andFalse
exactly or is any kind of truthy/falsy output allowed?(note that not all languages have a concept of
True
orFalse
, or might have different names for them) – Sp3000 – 2016-02-07T10:28:51.837This looks familiar, but I can't find the one I'm thinking of. I think it might've been in Code Review SE, so not a golfing challenge. I'm also pretty sure I've seen it given as a job interview question more than once, though they probably wouldn't appreciate you giving them any of the answers below in response... – Darrel Hoffman – 2016-02-07T21:21:06.297
@Geobits Well I screwed that up – Angzuril – 2016-02-07T23:16:44.407
It is a classic homework question in introductory programming classes so you may have seen it there. @Sp3000 Anything Truthy or Falsy in the output might make this a little too easy. I leave it up to those with more experience to decide – Mir – 2016-02-07T23:30:51.503