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.
  • OriginalGriffO OriginalGriff

    First guess on an algorithm: Grab a nut at random and test all bolts against it to form two piles "bigger than" and "smaller than" plus one bolt "same as". Now use the matching bolt to do the same thing for all nuts to form two piles. Process each pile the same way, to get 4 piles of nuts, 4 piles of bolts (and two matching pairs) Repeat. My gut feeling is that it'll be a lot quicker than a "brute force" compare all: it's kinda using QuickSort to match 'em up.

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

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

    If you always go back and compare with the already-matched sets, I can see that.

    1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      First guess on an algorithm: Grab a nut at random and test all bolts against it to form two piles "bigger than" and "smaller than" plus one bolt "same as". Now use the matching bolt to do the same thing for all nuts to form two piles. Process each pile the same way, to get 4 piles of nuts, 4 piles of bolts (and two matching pairs) Repeat. My gut feeling is that it'll be a lot quicker than a "brute force" compare all: it's kinda using QuickSort to match 'em up.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

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

      Well, that would have been my method too.

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

      1 Reply Last reply
      0
      • P PIEBALDconsult

        I'd be interested in seeing the test data set. :-D But more importantly, I'd like a more detailed spec. Are there both left- and right- handed nuts and bolts? Are there nuts and bolts with the same diameter but different pitch (or whatever)? Are there nuts and bolts with incompatible composition? (E.g. leading to electrolysis or similar?)

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

        Whitworth or metric. How about cap or flange? Do we care about hex bolts or Phillips? (Or Robertson for the Canadians)

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

        realJSOPR D 2 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          First guess on an algorithm: Grab a nut at random and test all bolts against it to form two piles "bigger than" and "smaller than" plus one bolt "same as". Now use the matching bolt to do the same thing for all nuts to form two piles. Process each pile the same way, to get 4 piles of nuts, 4 piles of bolts (and two matching pairs) Repeat. My gut feeling is that it'll be a lot quicker than a "brute force" compare all: it's kinda using QuickSort to match 'em up.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

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

          But I think the devil might be in the details here, how would you implement the collection?

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

          OriginalGriffO 1 Reply Last reply
          0
          • J Jorgen Andersson

            But I think the devil might be in the details here, how would you implement the collection?

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

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #8

            Binary tree, with two lists per node, probably - but it would likely depend on the language (and the size of N: it would probably be worth adding a test on that and brute-forcing low values as the memory allocations wouldn't be that cheap in time terms). An assembler solution wouldn't look anything like a C# implementation! :laugh:

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            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

              S Offline
              S Offline
              Slacker007
              wrote on last edited by
              #9

              42

              R 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

                T Offline
                T Offline
                theoldfool
                wrote on last edited by
                #10

                Are nuts metric?

                If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.

                OriginalGriffO P J 4 Replies Last reply
                0
                • OriginalGriffO OriginalGriff

                  Binary tree, with two lists per node, probably - but it would likely depend on the language (and the size of N: it would probably be worth adding a test on that and brute-forcing low values as the memory allocations wouldn't be that cheap in time terms). An assembler solution wouldn't look anything like a C# implementation! :laugh:

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

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

                  Binary tree maybe, I was thinking of an ordered List and a binary search.

                  1 Reply Last reply
                  0
                  • T theoldfool

                    Are nuts metric?

                    If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #12

                    Only Cash-ews.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    P 1 Reply Last reply
                    0
                    • T theoldfool

                      Are nuts metric?

                      If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.

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

                      Mine aren't.

                      T 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        Only Cash-ews.

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

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

                        (Cashews are seeds.)

                        OriginalGriffO 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          (Cashews are seeds.)

                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #15

                          Most nuts are seeds. As Brian O'Driscoll said: "Knowledge is knowing that a tomato is a fruit. Wisdom is knowing not to put it in a fruit salad." Cashews are still good in a stir fry or curry! :laugh:

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            Mine aren't.

                            T Offline
                            T Offline
                            theoldfool
                            wrote on last edited by
                            #16

                            These programming contests are difficult. I think I will go to QA and ask for the codez.

                            If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.

                            1 Reply Last reply
                            0
                            • J Jorgen Andersson

                              Whitworth or metric. How about cap or flange? Do we care about hex bolts or Phillips? (Or Robertson for the Canadians)

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

                              realJSOPR Online
                              realJSOPR Online
                              realJSOP
                              wrote on last edited by
                              #17

                              the only thing that matters in terms of "matching are pitch and diameter. The type of head is irrelevant.

                              ".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 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                First guess on an algorithm: Grab a nut at random and test all bolts against it to form two piles "bigger than" and "smaller than" plus one bolt "same as". Now use the matching bolt to do the same thing for all nuts to form two piles. Process each pile the same way, to get 4 piles of nuts, 4 piles of bolts (and two matching pairs) Repeat. My gut feeling is that it'll be a lot quicker than a "brute force" compare all: it's kinda using QuickSort to match 'em up.

                                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                                P Offline
                                P Offline
                                Peter_in_2780
                                wrote on last edited by
                                #18

                                My first thought too. Somewhere I have a book describing the historical development of heapsort, and I suspect a lot of the nuts and bolts could be found there (awful pun intended). [edit] On refection, and now with a measurable caffeine content, bits of Quicksort may be more relevant. Pivoting... [/edit]

                                Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                                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

                                  realJSOPR Online
                                  realJSOPR Online
                                  realJSOP
                                  wrote on last edited by
                                  #19

                                  Most of the work was getting the collections of nuts and bolts built. This code builds a collection of 10 nuts with randomly selected diameter and pitch, and then builds a random list of bolts from the list of nuts. These collections assume that each nut with have a unique diameter and pitch combination, and that each nut as a matching bolt. Finally, I simply sort both lists on diameter, and present the pairs by iterating the nuts list (without doing any comparison for diameter and pitch).

                                  using System;
                                  using System.Collections.Generic;

                                  namespace ConsoleApp3
                                  {
                                  class Program
                                  {
                                  static void Main(string[] args)
                                  {
                                  // prep the data
                                  Parts nuts = new Parts();
                                  Parts bolts = new Parts(nuts);

                                          nuts.Sort();
                                          bolts.Sort();
                                  
                                          foreach(Part nut in nuts)
                                          {
                                              Part bolt = bolts\[nuts.IndexOf(nut)\];
                                              Console.WriteLine("Pair: \[{0}\] - \[{1}\]", nut, bolt);
                                          }
                                          Console.ReadKey();
                                      }
                                  }
                                  
                                  public enum HardwareType { BOLT=0, NUT}
                                  
                                  /// The "part" (all parts have a hardware type, a diameter and pitch)
                                  public class Part : IComparable
                                  {
                                      public HardwareType Hardware { get; set; }
                                      public int ItemID { get; set; }
                                      public int Diameter { get; set; }
                                      public int Pitch { get; set; }
                                      public Part(int itemID, int diameter, int pitch, HardwareType hardware)
                                      {
                                          this.ItemID   = itemID;
                                          this.Diameter = diameter;
                                          this.Pitch    = pitch;
                                          this.Hardware = hardware;
                                      }
                                  
                                      public int CompareTo(Part p)
                                      {
                                          return this.Diameter.CompareTo(p.Diameter);
                                      }
                                  
                                      // make it more convenient to look at in the debugger
                                      public override string ToString()
                                      {
                                          return string.Format("{0}, ID={1}, D={2}, P={3}", this.Hardware.ToString(), this.ItemID, this.Diameter, this.Pitch);
                                      }
                                  }
                                  
                                  public class Parts : List
                                  {
                                      // This constructor builds a collection of nuts
                                      public Parts(bool populate=true)
                                      {
                                          if (populate)
                                          {
                                              // create a list of "part"s with randomly selected combinations of diameter and pitch
                                              List diameters = new List(){  1, 2, 3, 4, 5, 6, 7, 8, 9,10 };
                                              List pitches   = new List(){ 11,12,13,14,15,16,17,18,19,20 };
                                  
                                  D 1 Reply Last reply
                                  0
                                  • realJSOPR realJSOP

                                    the only thing that matters in terms of "matching are pitch and diameter. The type of head is irrelevant.

                                    ".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
                                    #20

                                    #realJSOP wrote:

                                    The type of head is irrelevant.

                                    I'm unable to keep it KSS. :sigh:

                                    1 Reply Last reply
                                    0
                                    • J Jorgen Andersson

                                      Whitworth or metric. How about cap or flange? Do we care about hex bolts or Phillips? (Or Robertson for the Canadians)

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

                                      D Offline
                                      D Offline
                                      DRHuff
                                      wrote on last edited by
                                      #21

                                      Robertson is a screw. Not a bolt.

                                      If you can't laugh at yourself - ask me and I will do it for you.

                                      1 Reply Last reply
                                      0
                                      • realJSOPR realJSOP

                                        Most of the work was getting the collections of nuts and bolts built. This code builds a collection of 10 nuts with randomly selected diameter and pitch, and then builds a random list of bolts from the list of nuts. These collections assume that each nut with have a unique diameter and pitch combination, and that each nut as a matching bolt. Finally, I simply sort both lists on diameter, and present the pairs by iterating the nuts list (without doing any comparison for diameter and pitch).

                                        using System;
                                        using System.Collections.Generic;

                                        namespace ConsoleApp3
                                        {
                                        class Program
                                        {
                                        static void Main(string[] args)
                                        {
                                        // prep the data
                                        Parts nuts = new Parts();
                                        Parts bolts = new Parts(nuts);

                                                nuts.Sort();
                                                bolts.Sort();
                                        
                                                foreach(Part nut in nuts)
                                                {
                                                    Part bolt = bolts\[nuts.IndexOf(nut)\];
                                                    Console.WriteLine("Pair: \[{0}\] - \[{1}\]", nut, bolt);
                                                }
                                                Console.ReadKey();
                                            }
                                        }
                                        
                                        public enum HardwareType { BOLT=0, NUT}
                                        
                                        /// The "part" (all parts have a hardware type, a diameter and pitch)
                                        public class Part : IComparable
                                        {
                                            public HardwareType Hardware { get; set; }
                                            public int ItemID { get; set; }
                                            public int Diameter { get; set; }
                                            public int Pitch { get; set; }
                                            public Part(int itemID, int diameter, int pitch, HardwareType hardware)
                                            {
                                                this.ItemID   = itemID;
                                                this.Diameter = diameter;
                                                this.Pitch    = pitch;
                                                this.Hardware = hardware;
                                            }
                                        
                                            public int CompareTo(Part p)
                                            {
                                                return this.Diameter.CompareTo(p.Diameter);
                                            }
                                        
                                            // make it more convenient to look at in the debugger
                                            public override string ToString()
                                            {
                                                return string.Format("{0}, ID={1}, D={2}, P={3}", this.Hardware.ToString(), this.ItemID, this.Diameter, this.Pitch);
                                            }
                                        }
                                        
                                        public class Parts : List
                                        {
                                            // This constructor builds a collection of nuts
                                            public Parts(bool populate=true)
                                            {
                                                if (populate)
                                                {
                                                    // create a list of "part"s with randomly selected combinations of diameter and pitch
                                                    List diameters = new List(){  1, 2, 3, 4, 5, 6, 7, 8, 9,10 };
                                                    List pitches   = new List(){ 11,12,13,14,15,16,17,18,19,20 };
                                        
                                        D Offline
                                        D Offline
                                        DRHuff
                                        wrote on last edited by
                                        #22

                                        And how does the sort work since the problem states that you can’t compare nut to nut or bolt to bolt?

                                        If you can't laugh at yourself - ask me and I will do it for you.

                                        realJSOPR 1 Reply Last reply
                                        0
                                        • OriginalGriffO OriginalGriff

                                          First guess on an algorithm: Grab a nut at random and test all bolts against it to form two piles "bigger than" and "smaller than" plus one bolt "same as". Now use the matching bolt to do the same thing for all nuts to form two piles. Process each pile the same way, to get 4 piles of nuts, 4 piles of bolts (and two matching pairs) Repeat. My gut feeling is that it'll be a lot quicker than a "brute force" compare all: it's kinda using QuickSort to match 'em up.

                                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                                          D Offline
                                          D Offline
                                          DRHuff
                                          wrote on last edited by
                                          #23

                                          Is there an implied constraint to stop testing once a nut and bolt match? In that case you have 3 piles (usually) at the end of the first sort. Big small and untested.

                                          If you can't laugh at yourself - ask me and I will do it for you.

                                          OriginalGriffO 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