How would you solve this 'interview' question in D?
MattCoder
mattcoder at hotmail.com
Wed Jun 26 16:52:11 PDT 2013
On Wednesday, 26 June 2013 at 22:43:05 UTC, David wrote:
> Am 26.06.2013 22:51, schrieb Gary Willoughby:
> I solved it ;)
>
> http://dpaste.dzfl.pl/5cd56e9d
Yes, but "maybe" the interviewer is waiting just one function,
since the question was: "Design a function f". So I rewrote your
example:
import std.stdio;
import std.string;
auto f(T)(T n){
if(is(T==float))
return cast(int)n;
return cast(float)-n;
}
void main() {
foreach(n; int.min..int.max) {
assert(f(f(n)) == -n);
}
}
Again, in the same way, maybe the interviewer want "n" as 32
signed int not a auto/template etc. :D
More information about the Digitalmars-d-learn
mailing list