5 ms·
Definitely a brainteaser if a valid solution is a hacky one like at http://stackoverflow.com/a/732049/827437 http://stackoverflow.com/a/732049/827437 : def f
by narsil 13y ago
Definitely a brainteaser if a valid solution is a hacky one like at http://stackoverflow.com/a/732049/827437 http://stackoverflow.com/a/732049/827437 :
def f(x):
if isinstance(x,int):
return (lambda: -x)
else:
return x()
- viraptor 13y agoIs it a hacky solution? Where's the line between a hacky one and a normal one?
- agravier 13y agoI think that the leakage of abstraction is mathematically impure, in his view.
- bsaul 13y agowell, n is supposed to be a 32 bits integer int. Not a lambda. if f(n) returns a lambda, it can't be composed with f again, according to the strict definition of "n".
- viraptor 13y agoAnd it is. The question is: f(f(n)) == -n Where n is a 32 bit signed integer It's `f(n)` that is not a signed integer. Both `n` and `f(f(n))` are ints, so the answer is within the constraints. There's nothing said about the signature of the function.
- bsaul 13y agothat seems a bit far fetched. the way i understand n, it means " f input ", as "the parameter that f takes as input, whose type is an integer". Anyway, that's probably just one way of reading the question. Really depends on what the interviewer's intention was.
- petercooper 13y agoTherein lies a lesson for the interviewer, "your spec is too vague" :-)