Click here to Skip to main content
15,879,535 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Nuts and bolts - Programming contest Pin
Kornfeld Eliyahu Peter28-Dec-20 23:12
professionalKornfeld Eliyahu Peter28-Dec-20 23:12 
GeneralRe: Nuts and bolts - Programming contest Pin
Sander Rossel29-Dec-20 3:20
professionalSander Rossel29-Dec-20 3:20 
GeneralRe: Nuts and bolts - Programming contest Pin
Harrison Pratt29-Dec-20 4:20
professionalHarrison Pratt29-Dec-20 4:20 
GeneralRe: Nuts and bolts - Programming contest Pin
Harrison Pratt29-Dec-20 5:07
professionalHarrison Pratt29-Dec-20 5:07 
GeneralRe: Nuts and bolts - Programming contest Pin
Dan Sutton29-Dec-20 6:15
Dan Sutton29-Dec-20 6:15 
GeneralRe: Nuts and bolts - Programming contest Pin
Kirk 1038982129-Dec-20 10:41
Kirk 1038982129-Dec-20 10:41 
GeneralRe: Nuts and bolts - Programming contest Pin
James Lonero29-Dec-20 15:00
James Lonero29-Dec-20 15:00 
GeneralRe: Nuts and bolts - Programming contest Pin
PIEBALDconsult30-Dec-20 12:59
mvePIEBALDconsult30-Dec-20 12:59 
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.

0) Begin with a pile of all of the Nuts on the "null" Tray and a pile of all the of Bolts.
1) Draw one Bolt from the pile of Bolts.
2) 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.
3) Create a new Tray, put the Bolt on it, shift any "larger" Trays to the right, and insert the new Tray.
4) 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 are.
5) If there are more Bolts to match, repeat from 1)
6) Done.


For this implementation, in place of the "size" of a Nut or a Bolt, I use the index of them in the input array, but this should prove equivalent.
These are guaranteed to be unique, non-negative integers and allows for matching more complex items. It also allows duplicates if the user wishes.

C#
namespace PIEBALD
{
  public static partial class Tester
  {
    public static int
    Main
    (
      string[] args
    )
    {
      int result = 0 ;

      /* "1/8"" "1/4"" "3/8"" "1/2"" "5/8"" "3/4"" "7/8"" "1"" */
      System.Collections.Generic.IList<NutAndBolt.Pair> list = NutAndBolt.Run ( args ) ;

      for ( int i = 0 ; i < list.Count ; i++ )
      {
        System.Console.WriteLine
        (
          "{0,2} : {1}"
        ,
          i
        ,
          list [ i ]
        ) ;
      }

      return ( result ) ;
    }
  }

  public static partial class NutAndBolt
  {
    public sealed partial class Pair
    {
      public Nut  Nut  { get ; private set ; }
      public Bolt Bolt { get ; private set ; }

      public Pair
      (
        Nut  Nut
      ,
        Bolt Bolt
      )
      {
        if ( Nut.CompareTo ( Bolt ) != 0 )
        {
          throw ( new System.Exception ( "" ) ) ;
        }

        this.Nut  = Nut  ;
        this.Bolt = Bolt ;

        return ;
      }

      public override string
      ToString
      (
      )
      {
        return ( System.String.Format
        ( 
          "{0} Nut and {1} Bolt"
        ,
          this.Nut.Description
        ,
          this.Bolt.Description
        ) ) ;
      }
    }

    public static System.Collections.Generic.IList<Pair>
    Run
    (
      params string[] Description
    )
    {
      System.Collections.Generic.List<Pair> result = 
        new System.Collections.Generic.List<Pair> ( Description.Length ) ;

      BoltList boltlist = new BoltList ( Description.Length ) ;

      TrayList traylist = new TrayList ( Description.Length ) ;

      NutList nutlist = traylist [ 0 ].NutList ;

      for ( int i = 0 ; i < Description.Length ; i++ )
      {
        nutlist.Add  ( new Nut  ( i , Description [ i ] ) ) ;
        boltlist.Add ( new Bolt ( i , Description [ i ] ) ) ;
      }

      nutlist.Shuffle() ;
      boltlist.Shuffle() ;

      Resolve ( traylist , boltlist ) ;

      for ( int i = 1 ; i < traylist.Count ; i++ )
      {
        result.Add ( new Pair ( traylist [ i ].Nut , traylist [ i ].Bolt ) ) ;
      }

      return ( result.AsReadOnly() ) ;
    }

    private static int
    Resolve
    (
      TrayList TrayList
    ,
      BoltList BoltList
    )
    {
      for ( int b = BoltList.Count - 1 ; b >= 0 ; b-- )
      {
        int t = TrayList.Insert ( BoltList [ b ] ) ;

        Tray tray = TrayList [ t ] ;

        NutList nutlist = TrayList [ t - 1 ].NutList ;

        for ( int n = nutlist.Count - 1 ; n >= 0 ; n-- )
        {
          int d = nutlist [ n ].CompareTo ( tray.Bolt ) ;

          if ( d > 0 )
          {
            tray.NutList.Add ( nutlist.Fetch ( n ) ) ;
          }
          else if ( d == 0 )
          {
            tray.AddNut ( nutlist.Fetch ( n ) ) ;
          }
        }
      }

      return ( TrayList.Count ) ;
    }

    public abstract partial class Base
    {
      private int    ID          ;
      public  string Description { get ; private set ; }

      protected Base
      (
        int    ID 
      ,
        string Description 
      )
      {
        if ( System.String.IsNullOrWhiteSpace ( Description ) && ( ID != -1 ) )
        {
          throw ( new System.Exception ( "" ) ) ;
        }

        this.ID          = ID          ;
        this.Description = Description ;

        return ;
      }

      protected static int
      Compare
      (
        Base Op0
      ,
        Base Op1
      )
      {
        return ( Op0.ID.CompareTo ( Op1.ID ) ) ;
      }
    }

    private abstract partial class BaseList<T> : System.Collections.Generic.List<T> 
      where T : Base
    {
      protected BaseList
      (
        int Capacity
      )
      : base
      (
        Capacity
      )
      {
        return ;
      }

      public virtual void
      Shuffle
      (
      )
      {
        for ( int i = this.Count ; i > 0 ; i-- )
        {
          int j = Random.Next ( i ) ;

          T n = this [ j ] ;

          this.RemoveAt ( j ) ;

          this.Add ( n ) ;
        }

        return ;
      }

      /* Removes and returns the specified item */
      protected virtual B
      Fetch<B>
      (
        int Index
      )
      where B : Base
      {
        B result = this [ Index ] as B ;

        this.RemoveAt ( Index ) ;

        return ( result ) ;
      }
    }

    public sealed partial class Nut : Base , System.IComparable<Bolt>
    {
      public Nut
      (
        int    ID 
      ,
        string Descrption 
      )
      : base
      (
        ID 
      ,
        Descrption 
      )
      {
        return ;
      }

      public int
      CompareTo
      (
        Bolt Bolt
      )
      {
        return ( Compare ( this , Bolt ) ) ;
      }
    }

    private sealed partial class NutList : BaseList<Nut> 
    {
      public NutList
      (
        int Capacity
      )
      : base
      (
        Capacity
      )
      {
        return ;
      }

      /* Removes and returns the specified Nut */
      public Nut
      Fetch
      (
        int Index
      )
      {
        return ( this.Fetch<Nut> ( Index ) ) ;
      }
    }

    public sealed partial class Bolt : Base , System.IComparable<Nut>
    {
      public Bolt
      (
        int    ID 
      ,
        string Descrption 
      )
      : base
      (
        ID 
      ,
        Descrption 
      )
      {
        return ;
      }

      public int
      CompareTo
      (
        Nut Nut
      )
      {
        return ( Compare ( this , Nut ) ) ;
      }
    }

    private sealed partial class BoltList : BaseList<Bolt> 
    {
      public BoltList
      (
        int Capacity
      )
      : base
      (
        Capacity
      )
      {
        return ;
      }

      /* Removes and returns the specified Bolt */
      public Bolt
      Fetch
      (
        int Index
      )
      {
        return ( this.Fetch<Bolt> ( Index ) ) ;
      }
    }

    private sealed partial class Tray
    {
      public Nut     Nut     { get ; private set ; }
      public Bolt    Bolt    { get ; private set ; }

      public NutList NutList { get ; private set ; }

      public Tray
      (
        Bolt Bolt
      ,
        int Capacity
      )
      {
        if ( Bolt == null ) throw ( new System.Exception() ) ;

        this.Bolt = Bolt ;

        this.NutList = new NutList ( Capacity ) ;

        return ;
      }

      public void
      AddNut
      (
        Nut Nut
      )
      {
        if ( Nut == null ) throw ( new System.Exception() ) ;
        if ( this.Nut != null ) throw ( new System.Exception() ) ;

        this.Nut = Nut ;

        return ;
      }
    }

    private sealed partial class TrayList
    {
      private System.Collections.Generic.List<Tray> list ;

      public TrayList
      (
        int Capacity
      )
      {
        this.list = new System.Collections.Generic.List<Tray> ( Capacity ) ;

        /* Tray 0 is the "null tray" */
        Tray tray = new Tray ( new Bolt ( -1 , null ) , Capacity ) ;
        tray.AddNut ( new Nut ( -1 , null ) ) ;
        this.list.Add ( tray ) ;

        return ;
      }

      public int
      Count
      {
        get
        {
          return ( this.list.Count ) ;
        }
      }

      /* TrayList is sorted by Nut and Bolt size */
      public int
      Insert
      (
        Bolt Bolt
      )
      {
        int result = 0 ;

        if ( Bolt == null ) throw ( new System.Exception() ) ;

        /* Could be replaced with a binary search if we expected a large number of Trays */
        while
        (
          ( result < this.Count )
        && 
          ( this.list [ result ].Nut.CompareTo ( Bolt ) < 0 )
        )
        {
          result++ ;
        }        

        this.list.Insert ( result , new Tray ( Bolt , this.list [ result - 1 ].NutList.Count ) ) ;

        return ( result ) ;
      }

      public Tray
      this
      [
        int Index
      ]
      {
        get
        {
          return ( this.list [ Index ] ) ;
        }
      }
    }

    private static partial class Random
    {
      private static System.Random rnd ;

      static Random
      (
      )
      {
        rnd = new System.Random ( (int) System.DateTime.Now.Ticks ) ;

        return ;
      }

      public static int
      Next
      (
        int Max
      )
      {
        for ( int i = System.DateTime.Now.Millisecond / 100 ; i > 0 ; i-- )
        {
          rnd.Next() ;
        }

        return ( rnd.Next ( Max ) ) ;
      }
    }
  }
}

GeneralRe: Nuts and bolts - Programming contest Pin
OldDBA31-Dec-20 3:56
OldDBA31-Dec-20 3:56 
GeneralRe: Nuts and bolts - Programming contest Pin
PIEBALDconsult31-Dec-20 12:22
mvePIEBALDconsult31-Dec-20 12:22 
GeneralThought of the Day Pin
OriginalGriff28-Dec-20 4:39
mveOriginalGriff28-Dec-20 4:39 
GeneralRe: Thought of the Day Pin
W Balboos, GHB28-Dec-20 5:41
W Balboos, GHB28-Dec-20 5:41 
GeneralRe: Thought of the Day Pin
jeron128-Dec-20 5:57
jeron128-Dec-20 5:57 
GeneralRe: Thought of the Day Pin
Johnny J.28-Dec-20 21:56
professionalJohnny J.28-Dec-20 21:56 
GeneralRe: Thought of the Day Pin
jeron129-Dec-20 8:10
jeron129-Dec-20 8:10 
GeneralJust one well placed virtual function saves the day Pin
honey the codewitch28-Dec-20 4:08
mvahoney the codewitch28-Dec-20 4:08 
GeneralRe: Just one well placed virtual function saves the day Pin
Josh Gray228-Dec-20 4:57
Josh Gray228-Dec-20 4:57 
GeneralRe: Just one well placed virtual function saves the day Pin
honey the codewitch28-Dec-20 5:18
mvahoney the codewitch28-Dec-20 5:18 
GeneralRe: Just one well placed virtual function saves the day Pin
DRHuff28-Dec-20 5:27
DRHuff28-Dec-20 5:27 
GeneralRe: Just one well placed virtual function saves the day Pin
honey the codewitch28-Dec-20 5:33
mvahoney the codewitch28-Dec-20 5:33 
GeneralSynchro/Servo? Stepper? What? Pin
GenJerDan28-Dec-20 0:11
GenJerDan28-Dec-20 0:11 
GeneralRe: Synchro/Servo? Stepper? What? Pin
Mircea Neacsu28-Dec-20 0:42
Mircea Neacsu28-Dec-20 0:42 
GeneralRe: Synchro/Servo? Stepper? What? Pin
GenJerDan28-Dec-20 1:05
GenJerDan28-Dec-20 1:05 
GeneralRe: Synchro/Servo? Stepper? What? Pin
glennPattonWork328-Dec-20 2:43
professionalglennPattonWork328-Dec-20 2:43 
GeneralRe: Synchro/Servo? Stepper? What? Pin
GenJerDan29-Dec-20 0:13
GenJerDan29-Dec-20 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.