Interview follow up
-
I think this should do it:
private int BinaryPeriod(int num)
{
int result = -1;
int numBits = (int)((Math.Log(num) / Math.Log(2))) + 1;for (int i = 1; i <= numBits / 2 && result == -1; i++) { if (((num & ((1 << (numBits - i)) - 1)) ^ (num >> i)) == 0) result = i; } return result;
}
I've done it in C#, but hopefully it should be fairly generic code. The important line is:
if (((num & ((1 << (numBits - i)) - 1)) ^ (num >> i)) == 0)
which strips the top i bits and XORs that with num right shifted. If the result is 0 then we have found an autocorrelation.
Jon CodeWrite
Very good! Wish I knew of it :(
-
I think this should do it:
private int BinaryPeriod(int num)
{
int result = -1;
int numBits = (int)((Math.Log(num) / Math.Log(2))) + 1;for (int i = 1; i <= numBits / 2 && result == -1; i++) { if (((num & ((1 << (numBits - i)) - 1)) ^ (num >> i)) == 0) result = i; } return result;
}
I've done it in C#, but hopefully it should be fairly generic code. The important line is:
if (((num & ((1 << (numBits - i)) - 1)) ^ (num >> i)) == 0)
which strips the top i bits and XORs that with num right shifted. If the result is 0 then we have found an autocorrelation.
Jon CodeWrite
And here we have it in Scheme :) http://eval.ironscheme.net/?id=59[^] Thanks again!
-
Very good! Wish I knew of it :(
Thank you :) I love solving problems like this - although that has distracted me a bit from what I'm supposed to be doing ;P Good luck with the job!
Jon CodeWrite
-
Was given a problem that was suppose to come from the 'easy' bin. Two hours later I was still not finished... On the plus side, the interviewer attempted the problem at the same time, and also got stuck :) He said the problem must have been miscategorized. He told me to finish at home and to send the solution when done ;p Anyways, after a good nights sleep (with some wack apocalyptic dream I had), I started from scratch and coded a solution in about an hour this morning. The given problem: Binary period (dont bother googling for answers as you will likely just end up getting astronomy results or if you have google foo, a few very mathematical papers mostly related to cryptography) Given a number N larger than 0 (range was given, but does not matter), find the minimum period of repeating bit sequences or -1 if none is found. Examples
110110110 => 3 (as 110 repeats)
11011011 => 3 (still 3 and valid as the remaining subset, 11, is in 110)
110110111 => -1 (no period)
101 => -1 (no repeats, repeat implies at least 2 sequences)
10101 => 2 (repeat at least over 2 sequences)
11 => 110101010101 => 2
111011011101101110 => 7
111111111111111110 => -1My answer in Scheme can be viewed and run here[^]. Edit: Web host seems to be down now... Edit 2: Seems to be back up again, please only look one at a time ;p Damn you shared hosting. If above link is down, view here[^].
Heh, I had to do that in University.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
Heh, I had to do that in University.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
Was that some kind of homework or other task? Perhaps thesis? ;p
-
Was that some kind of homework or other task? Perhaps thesis? ;p
Well my degree was computer science. All we did was learn stuff like this. Wouldn't say I knew much about "programming" until after school. But solving problems with code, easy.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
Well my degree was computer science. All we did was learn stuff like this. Wouldn't say I knew much about "programming" until after school. But solving problems with code, easy.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
Not even in my 2nd year course (just before I decided to go back to working class) we did stuff like this... I left when when we were busy with Data Structures and Algorithms.