On Thu, Jun 27, 2013 at 12:43:04AM +0200, David wrote: > Am 26.06.2013 22:51, schrieb Gary Willoughby: > > Just for a bit of fun, I saw this question posted on reddit the other > > day and wondered how *you* would solve this in D? > > > > http://stackoverflow.com/questions/731832/interview-question-ffn-n > > I solved it ;) > > http://dpaste.dzfl.pl/5cd56e9d [...]
Well, technically it's invalid because it says "design *a* function f". But maybe it can be salvaged: auto f(T)(T n) { static if (is(T==int)) return cast(double)n; else if (is(T==double)) return -cast(int)n; else static assert(0); } Well, it's still cheating, though. :-P I think the 4-cycle algorithm is probably still the best one I've seen. A more interesting interview question, IMO, would be to design a function of the form: bool f(void delegate(int) dg, int n); which returns true if dg(n) returns, and false otherwise. :-P T -- Three out of two people have difficulties with fractions. -- Dirk Eddelbuettel