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. Nuts and bolts - Programming contest

Nuts and bolts - Programming contest

Scheduled Pinned Locked Moved The Lounge
htmlcomquestion
55 Posts 18 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.
  • realJSOPR realJSOP

    BTW, my sort version doesn't directly compare two nuts or two bolts. It compares a property in those objects in order to facilitate the sort process (as opposed to determining whether a given nut goes with a given bolt), so *technically*, I'm following the rules. Furthermore, I'm not matching a nut to a bolt via any kind of comparison. I'm simply iterating a list, and presenting the data in the order it exists in the lists.

    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
    -----
    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
    -----
    When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #37

    Yeah, I think you're cheating. :-D The nuts and bolts should be presented in random order.

    realJSOPR 1 Reply Last reply
    0
    • J Jorgen Andersson

      How about a little programming puzzle for the Holiday? I found this puzzle in a text by G. J. E. Rawlins. "You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts and bolts. Each nut matches exactly one bolt, and each bolt matches exactly one nut. By fitting a nut and bolt together, you can see which is bigger. But it is not possible to directly compare two nuts or two bolts." Selecting the winner will be heavily influenced by upvotes and Reactions™ :)

      Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

      M Offline
      M Offline
      Member_5893260
      wrote on last edited by
      #38

      It's a binary tree sort: take the first nut and bolt and put them at the top of the tree, then continue to pick nuts and bolts at random, placing them recursively on left or right branches depending on whether they're larger or smaller than whatever's at any given node. Something like that. Matching nuts and bolts stay together. By the time you've tried all of them, they'll all have a match.

      1 Reply Last reply
      0
      • J Jorgen Andersson

        O(n * log(n)) would be an average, if you consistently select the wrong pivot you might end up with O(n2) :) It isn't just about the number of comparisons though, the number of swaps is also important

        Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #39

        Why would there be any swaps?

        J 1 Reply Last reply
        0
        • J Jorgen Andersson

          How about a little programming puzzle for the Holiday? I found this puzzle in a text by G. J. E. Rawlins. "You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts and bolts. Each nut matches exactly one bolt, and each bolt matches exactly one nut. By fitting a nut and bolt together, you can see which is bigger. But it is not possible to directly compare two nuts or two bolts." Selecting the winner will be heavily influenced by upvotes and Reactions™ :)

          Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

          K Offline
          K Offline
          Kirk 10389821
          wrote on last edited by
          #40

          So, if I have this right. I can tell if a Bolt+NUT is Bigger OR FITS or "not" meaning it is clearly smaller. The simplest algorithm is a bubble sort type loop/loop (O(n^2)).

          // Z = Number of nuts/bolts, N[1..Z], B[1..Z] hold the nuts/bots

          For X = 1 to Z
          For Y = 1 to Z
          If Fits(B[X],N[Y]) then F[X]=Y : Break;
          Next Y
          Next X

          For X = 1 to Z
          Print "Bolt " & X & " Fits with Nut " & F[X]
          Next X

          // Accepting that if the output is all that is needed, do the print in the main loop, before the break;

          for 0 or something to skip it if assigned

          // No optimizations here. But short, clear, concise.
          // Simplest optimization is to process the second loop in reverse, deleting an element as it is matched

          // The list of Nuts will shrink by one with each pass, cutting the comparisons in half.
          // Unfortunately requires a modifiable array.

          For X = 1 to Z
          For Y = Length(N) to 1 Step -1
          If Fits(B[X],N[Y]) then F[X]=Y : N.delete(Y) : Break;
          Next Y
          Next X

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Why would there be any swaps?

            J Offline
            J Offline
            Jorgen Andersson
            wrote on last edited by
            #41

            If you're sorting something you need to swap elements in the collection, right?

            Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

            P 1 Reply Last reply
            0
            • J Jorgen Andersson

              If you're sorting something you need to swap elements in the collection, right?

              Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #42

              No. And it's not sorting either. More like inserting into a sorted list -- at least that's what I'm doing.

              J 1 Reply Last reply
              0
              • J Jorgen Andersson

                How about a little programming puzzle for the Holiday? I found this puzzle in a text by G. J. E. Rawlins. "You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts and bolts. Each nut matches exactly one bolt, and each bolt matches exactly one nut. By fitting a nut and bolt together, you can see which is bigger. But it is not possible to directly compare two nuts or two bolts." Selecting the winner will be heavily influenced by upvotes and Reactions™ :)

                Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                J Offline
                J Offline
                James Lonero
                wrote on last edited by
                #43

                Assuming a mixed pile of nuts and bolts where 1. Each nut matches exactly one bolt. 2. Must fit a nut and bolt together to compare “which is bigger” (size) 3. Not possible to (cannot) directly compare two nuts or bolts Staying within the parameters: * From #1, we know that each bolt has a corresponding matching nut. Then the matching bolt and nut are eliminated from further searches. * From #2, ‘bigger’ assumes size. Not head configuration, pitch, left or right-handed, or composition. Also, since we need to fit the nut onto the bolt, we are only concerned about the width/diameter (of the bolt and nut) and not the length. * From #3, we cannot compare the nuts or the bolts therefore, we are not allowed to sort them. (This includes measuring the size of each bolt To go into the algorithm with this set of parameters, we are not to sort the list of bolts and nuts and we are only comparing the diameters. So, we know that we have a set of N nuts to match up with a similar number of bolts. Here is a simple algorithm:

                int boltDiameters[n] // Resizable array
                int nutDiameters[n] // Resizable array

                do {
                boltsize = boltDiameters[0]
                for (int i = 0; i < nutDiameters.Length; i++) {
                if (nutDiameters[i] == boltsize) {
                println(“Found nut-bolt with size of: “ + boltsize)
                nutDiameters.RemoveAt(i)
                boltDiameters.RemoveAt(0)
                }
                }
                } Until (boltDiameters.Length == 0)

                Each iteration through the loop, we reduce the arrays by one element. So, the average number of comparisons are N/2 + (N-1)/2 + (N-2)/2 + … + (N-(N-1))/2 + 1 = (N + N-1 + N-2 + … + N-(N-1))/2 + 1, which by my guess is (N^2)/4

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  No. And it's not sorting either. More like inserting into a sorted list -- at least that's what I'm doing.

                  J Offline
                  J Offline
                  Jorgen Andersson
                  wrote on last edited by
                  #44

                  Ah, but I was referring to Griffs original comment:

                  Griff wrote:

                  it's kinda using QuickSort to match 'em up.

                  That would most probably use swapping of elements, and that's where quicksort is excelling by doing fewer of them than most sorting algorithms.

                  Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                  P 1 Reply Last reply
                  0
                  • J Jorgen Andersson

                    Ah, but I was referring to Griffs original comment:

                    Griff wrote:

                    it's kinda using QuickSort to match 'em up.

                    That would most probably use swapping of elements, and that's where quicksort is excelling by doing fewer of them than most sorting algorithms.

                    Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #45

                    True, but this particular challenge doesn't require sorting or swapping. And at one point I thought he was talking about inserting to a binary tree.

                    J 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      True, but this particular challenge doesn't require sorting or swapping. And at one point I thought he was talking about inserting to a binary tree.

                      J Offline
                      J Offline
                      Jorgen Andersson
                      wrote on last edited by
                      #46

                      I'm looking forward to see your solution

                      Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                      P 2 Replies Last reply
                      0
                      • P PIEBALDconsult

                        Yeah, I think you're cheating. :-D The nuts and bolts should be presented in random order.

                        realJSOPR Offline
                        realJSOPR Offline
                        realJSOP
                        wrote on last edited by
                        #47

                        That wasn't stated as a requirement, however I did create a random collection of each. My example also assumes that there will only be one occurrence of each diameter and pitch, but changing that will only affect the sort comparison method, which everyone seems to think is not valid.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                        P J 2 Replies Last reply
                        0
                        • J Jorgen Andersson

                          I'm looking forward to see your solution

                          Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #48

                          Just refactoring, reordering methods now, trying to make it at least a little more understandable. Last night I tried making a big change, but it didn't work. The thing is, it wound up being more code than I expected -- a bunch of classes to support a fairly simple algorithm.

                          1 Reply Last reply
                          0
                          • realJSOPR realJSOP

                            That wasn't stated as a requirement, however I did create a random collection of each. My example also assumes that there will only be one occurrence of each diameter and pitch, but changing that will only affect the sort comparison method, which everyone seems to think is not valid.

                            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                            -----
                            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                            -----
                            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #49

                            I think it's implied. You're handed a bag of nuts and a bag of bolts and you have to match them up.

                            1 Reply Last reply
                            0
                            • realJSOPR realJSOP

                              That wasn't stated as a requirement, however I did create a random collection of each. My example also assumes that there will only be one occurrence of each diameter and pitch, but changing that will only affect the sort comparison method, which everyone seems to think is not valid.

                              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                              -----
                              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                              -----
                              When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                              J Offline
                              J Offline
                              Jorgen Andersson
                              wrote on last edited by
                              #50

                              "You have a mixed pile of N nuts and N bolts" Also: "But it is not possible to directly compare two nuts or two bolts", so yes, it affects the comparison method quite a bit. But I really enjoy reading how you "renegotiate" the "rules". :-D

                              Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                              P 1 Reply Last reply
                              0
                              • J Jorgen Andersson

                                How about a little programming puzzle for the Holiday? I found this puzzle in a text by G. J. E. Rawlins. "You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts and bolts. Each nut matches exactly one bolt, and each bolt matches exactly one nut. By fitting a nut and bolt together, you can see which is bigger. But it is not possible to directly compare two nuts or two bolts." Selecting the winner will be heavily influenced by upvotes and Reactions™ :)

                                Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                                P Offline
                                P Offline
                                PIEBALDconsult
                                wrote on last edited by
                                #51

                                The algorithm I chose isn't all that complex and the code is fairly simple. It does rely on a number of classes, though, so there's a bit of code. I would not assign the implementation during an interview, but having a candidate describe the algorithm could be OK.

                                Algorithm:
                                I assume that separating the "pile of N nuts and N bolts" into a pile of nuts and a pile of bolts is allowed.
                                I wrote a Nut class and a Bolt class (both deriving from a Base class).
                                I wrote a NutList class and a BoltList class (both deriving from a BaseList class).
                                These hold a "pile" of Nuts or Bolts.
                                The lists can be Shuffled to simulate drawing one item at random, as you would from an actual pile.
                                If you were actually performing this task (during an interview perhaps), you might have some divided disposable plates on hand.
                                One common type of disposable plate for low-class dinner parties has a large section and two small sections.
                                I wrote a Tray class to represent this; it can hold one Nut, one Bolt, and one NutList (pile of Nuts).

                                Diagram of a Tray:
                                __________________________
                                | |
                                | |
                                | Pile of unmatched Nuts |
                                | |
                                |________________________|
                                | | |
                                | Matched | Matched |
                                | Nut | Bolt |
                                |____________|___________|

                                You could use a number of these disposable plates -- one for each nut-and-bolt you have matched up and that subset of the Nuts which are "larger" than them.
                                I wrote a TrayList class to hold an ordered list of Trays.
                                A TrayList begins with one item, containing a "null" nut-and-bolt, which is guaranteed to test "smaller" than all other Nuts and Bolts.

                                1. Begin with a pile of all of the Nuts on the "null" Tray and a pile of all the of Bolts.
                                2. Draw one Bolt from the pile of Bolts.
                                3. Beginning with the "null" Tray, compare the Bolt with the (matched) Nuts on the Trays in front of you.
                                  2.1) When you reach the end of the Trays or a Tray with a "larger" Nut, that's where you want to insert a new Tray for this Bolt.
                                4. Create a new Tray, put the Bolt on it, shift any "larger" Trays to the right, and insert the new Tray.
                                5. Test each Nut in the pile of (unmatched) Nuts on the Tray to the left of the new Tray against the Bolt.
                                  4.1) Any Nuts which are "larger" than the Bolt get moved to the new Tray's pile of (unmatched) Nuts.
                                  4.2) When you find the Nut which matches the Bolt, move it to the Tray.
                                  4.3) Any Nuts which are "smaller" than the Bolt remain where they
                                1 Reply Last reply
                                0
                                • J Jorgen Andersson

                                  I'm looking forward to see your solution

                                  Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #52

                                  Posted.

                                  1 Reply Last reply
                                  0
                                  • J Jorgen Andersson

                                    "You have a mixed pile of N nuts and N bolts" Also: "But it is not possible to directly compare two nuts or two bolts", so yes, it affects the comparison method quite a bit. But I really enjoy reading how you "renegotiate" the "rules". :-D

                                    Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #53

                                    : cough : .45 cal : cough :

                                    1 Reply Last reply
                                    0
                                    • J Jorgen Andersson

                                      How about a little programming puzzle for the Holiday? I found this puzzle in a text by G. J. E. Rawlins. "You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts and bolts. Each nut matches exactly one bolt, and each bolt matches exactly one nut. By fitting a nut and bolt together, you can see which is bigger. But it is not possible to directly compare two nuts or two bolts." Selecting the winner will be heavily influenced by upvotes and Reactions™ :)

                                      Wrong is evil and must be defeated. - Jeff Ello Never stop dreaming - Freddie Kruger

                                      O Offline
                                      O Offline
                                      OldDBA
                                      wrote on last edited by
                                      #54

                                      Test Data For nuts 1 7 4 2 3 5 4 9 8 For bolts 6 4 9 7 8 1 2 3 4 Does it meet the specifications. No, but then real data almost never is as the user said it would be. There are 2 matching 4s and one has an unmatched 5 the other an unmatched 6. Good data in > good data out Garbage in > report errors and depending on the situation quit or process what you can.

                                      P 1 Reply Last reply
                                      0
                                      • O OldDBA

                                        Test Data For nuts 1 7 4 2 3 5 4 9 8 For bolts 6 4 9 7 8 1 2 3 4 Does it meet the specifications. No, but then real data almost never is as the user said it would be. There are 2 matching 4s and one has an unmatched 5 the other an unmatched 6. Good data in > good data out Garbage in > report errors and depending on the situation quit or process what you can.

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #55

                                        Yeah, I should review mine to see what happens with unmatched items, but I don't think it will cause trouble. I know mine won't handle duplicates, but I could easily change it so that it does. Still, not required by the spec.

                                        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