Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Interview follow up

Interview follow up

Scheduled Pinned Locked Moved The Lounge
helpcsharpcomhostingquestion
27 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jonathan Nethercott

    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

    L Offline
    L Offline
    leppie
    wrote on last edited by
    #21

    Very good! Wish I knew of it :(

    IronScheme
    ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

    J 1 Reply Last reply
    0
    • J Jonathan Nethercott

      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

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #22

      And here we have it in Scheme :) http://eval.ironscheme.net/?id=59[^] Thanks again!

      IronScheme
      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

      1 Reply Last reply
      0
      • L leppie

        Very good! Wish I knew of it :(

        IronScheme
        ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

        J Offline
        J Offline
        Jonathan Nethercott
        wrote on last edited by
        #23

        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

        1 Reply Last reply
        0
        • L leppie

          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 => 1

          10101010101 => 2
          111011011101101110 => 7
          111111111111111110 => -1

          My 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[^].

          IronScheme
          ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #24

          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

          L 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            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

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #25

            Was that some kind of homework or other task? Perhaps thesis? ;p

            IronScheme
            ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

            E 1 Reply Last reply
            0
            • L leppie

              Was that some kind of homework or other task? Perhaps thesis? ;p

              IronScheme
              ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

              E Offline
              E Offline
              Ennis Ray Lynch Jr
              wrote on last edited by
              #26

              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

              L 1 Reply Last reply
              0
              • E Ennis Ray Lynch Jr

                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

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #27

                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.

                IronScheme
                ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups