June 26, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David | 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
|
June 26, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
On 6/27/13, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:
> Well, it's still cheating, though. :-P I think the 4-cycle algorithm is probably still the best one I've seen.
What I don't understand is why the CPU is so slow that it takes ages to go through int.min .. int.max in a loop.
|
June 27, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
On 6/27/13, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> On 6/27/13, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:
>> Well, it's still cheating, though. :-P I think the 4-cycle algorithm is probably still the best one I've seen.
>
> What I don't understand is why the CPU is so slow that it takes ages to go through int.min .. int.max in a loop.
>
I mean this takes 31 seconds on my machine (obviously without
optimization flags):
-----
int f(int n) { return n; }
void main()
{
foreach (n; int.min..int.max)
f(f(n));
}
-----
Maybe I need an upgrade.
|
June 27, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Wednesday, 26 June 2013 at 20:51:35 UTC, Gary Willoughby wrote:
> 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
Well, considering there is little to that specification:
int f(int num) {
import std.exception;
static called = false;
enforce(num != int.min);
if(!called) {
called = true;
return num;
} else {
called = false; // Only for unittesting
return -num;
}
}
unittest {
assert(f(f(3)) == -3);
assert(f(f(-3)) == 3);
assert(f(f(int.min+1)) == int.max);
assert(f(f(int.max)) == int.min+1);
}
int having the issue that not all numbers are representable in both +/-.
|
June 27, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Wednesday, 26 June 2013 at 21:00:42 UTC, Adam D. Ruppe wrote:
> On Wednesday, 26 June 2013 at 20:51:35 UTC, Gary Willoughby wrote:
>> 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?
>
> Since they didn't say *pure* function, I'd cheat:
>
> int f(int n) {
> static bool isOddCall;
> isOddCall = !isOddCall;
> if(!isOddCall)
> return -n;
> return n;
> }
>
> :P
Well if you solved it *that* way, *I* wouldn't hire you :p
|
June 27, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Wednesday, 26 June 2013 at 20:51:35 UTC, Gary Willoughby wrote:
> 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
The question is ambiguous as to what they mean by -n. Do they mean the result of negation on the 32bit signed int, or do they mean the negative of the number represented by that int. this matters because -int.min evaluates to int.min due to wraparound.
So.. If we assume we want the proper mathematical negation:
Taking note that although the spec specifies n as a 32bit integer it says nothing about the signature of the function or the type of the result, we can just use implicit int->long
byte s(long n)
{
return (n & 0x8000000000000000) ? -1 : 1;
}
long f(long x)
{
if(x == 0) return 0;
if(x % 2) return x + s(x);
return s(x) - x;
}
unittest
{
foreach(int n; int.min + 1 .. int.max)
{
assert(f(f(n)) == -n);
}
assert(f(f(int.max)) == -int.max);
}
|
June 27, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Thursday, 27 June 2013 at 12:38:25 UTC, John Colvin wrote: Woops, sorry missed an assert unittest { assert(f(f(int.min)) == -(cast(long)int.min)); foreach(int n; int.min + 1 .. int.max) { assert(f(f(n)) == -n); } assert(f(f(int.max)) == -int.max); } |
June 27, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Wednesday, 26 June 2013 at 23:14:09 UTC, H. S. Teoh wrote:
> I think the 4-cycle algorithm is
> probably still the best one I've seen.
All (correct) mathematically based answers are 4-cycle.
begin very sloppy proof with mixed up notation:
Let f^2(x) = -x
f^4(x) = f^2(f^2(x))
= f^2(-x)
= -(-x)
= x
Therefore:
f^4 = 1
f^{4n} = 1^n = 1, n in N
<==> f^{4n}(x) = x
□
|
June 27, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Thursday, 27 June 2013 at 12:38:25 UTC, John Colvin wrote: > The question is ambiguous as to what they mean by -n. Do they mean the result of negation on the 32bit signed int, or do they mean the negative of the number represented by that int. this matters because -int.min evaluates to int.min due to wraparound. From: February 11, 2008: Vlad Patryshev said... ...Write a function f on 32-bit integers that, applied twice, it negates the integer. f(f(n)) = -n. Source: http://steve-yegge.blogspot.com.br/2008/02/portrait-of-n00b.html Matheus. |
June 27, 2013 Re: How would you solve this 'interview' question in D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MattCodr | On Thursday, 27 June 2013 at 15:32:05 UTC, MattCodr wrote:
> On Thursday, 27 June 2013 at 12:38:25 UTC, John Colvin wrote:
>> The question is ambiguous as to what they mean by -n. Do they mean the result of negation on the 32bit signed int, or do they mean the negative of the number represented by that int. this matters because -int.min evaluates to int.min due to wraparound.
>
> From: February 11, 2008:
>
> Vlad Patryshev said...
>
> ...Write a function f on 32-bit integers that, applied twice, it negates the integer. f(f(n)) = -n.
>
> Source: http://steve-yegge.blogspot.com.br/2008/02/portrait-of-n00b.html
>
> Matheus.
How is that any more specific?
|
Copyright © 1999-2021 by the D Language Foundation