How would you solve this 'interview' question in D?
Marco Leise
Marco.Leise at gmx.de
Wed Jun 26 14:34:26 PDT 2013
Am Wed, 26 Jun 2013 22:52:17 +0200
schrieb "Gary Willoughby" <dev at kalekold.net>:
> The text from the question:
>
> Design a function f, such that:
>
> f(f(n)) == -n
> Where n is a 32 bit signed integer; you can't use complex numbers
> arithmetic.
>
> If you can't design such a function for the whole range of
> numbers, design it for the largest range possible.
Well some good answers are given that would work in D, too. I
like how this question turns out the nature of the person much
more than their general programming skill. Some options:
o downvote the question and close the tab
o face it with humor (e.g. throw NotImplementedException("You
get the rest of the code when you give me the job"))
o get dirty and hack around the issue, by actually having two
'f's (by means of C preprocessor abuse, or C++ overloads)
o be the pragmatic Python programmer: no overflows, no problems
o adhere strictly to the problem description and give multiple
solutions that do or don't handle 0 or -2^31.
Anyway I've yet to see a solution that works for all input ;)
--
Marco
More information about the Digitalmars-d-learn
mailing list