Click here to Skip to main content
15,890,438 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
Rusty Bullet29-Dec-20 3:08
Rusty Bullet29-Dec-20 3:08 
GeneralRe: Nuts and bolts - Programming contest Pin
theoldfool28-Dec-20 8:59
professionaltheoldfool28-Dec-20 8:59 
GeneralRe: Nuts and bolts - Programming contest Pin
OriginalGriff28-Dec-20 9:30
mveOriginalGriff28-Dec-20 9:30 
GeneralRe: Nuts and bolts - Programming contest Pin
PIEBALDconsult28-Dec-20 9:40
mvePIEBALDconsult28-Dec-20 9:40 
GeneralRe: Nuts and bolts - Programming contest Pin
OriginalGriff28-Dec-20 9:44
mveOriginalGriff28-Dec-20 9:44 
GeneralRe: Nuts and bolts - Programming contest Pin
PIEBALDconsult28-Dec-20 9:36
mvePIEBALDconsult28-Dec-20 9:36 
GeneralRe: Nuts and bolts - Programming contest Pin
theoldfool28-Dec-20 9:44
professionaltheoldfool28-Dec-20 9:44 
GeneralRe: Nuts and bolts - Programming contest Pin
#realJSOP28-Dec-20 11:53
mve#realJSOP28-Dec-20 11:53 
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).

C#
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<Part>
    {
        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<Part>
    {
        // 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<int> diameters = new List<int>(){  1, 2, 3, 4, 5, 6, 7, 8, 9,10 };
                List<int> pitches   = new List<int>(){ 11,12,13,14,15,16,17,18,19,20 };
                Random    diaRandom = new Random(1);
                Random    pitRandom = new Random(3);
                int diaIndex;
                int pitIndex;
                int id = 0;
                do
                {
                    diaIndex = diaRandom.Next(0, diameters.Count-1);
                    pitIndex = pitRandom.Next(0, pitches.Count-1);
                    this.Add(new Part(id, diameters[diaIndex], pitches[pitIndex], HardwareType.NUT));
                    diameters.RemoveAt(diaIndex);
                    pitches.RemoveAt(pitIndex);
                    id++;
                } while (diameters.Count > 1);
                this.Add(new Part(id, diameters[0], pitches[0], HardwareType.NUT));
                diameters.RemoveAt(0);
                pitches.RemoveAt(0);
            }
        }

        // this constructor build acollection of bolts based on the nuts collection provided.
        public Parts(Parts parts)
        {
            // we can't randoly select dia/pitch again, and we can't randmly select nuts and 
            // delete them from the list without destroying the original list, so we clone 
            // the colllection of nuts
            Parts cloned = new Parts(false);
            cloned.AddRange(new List<Part>(parts));

            // and now we can rendomly select a nut from the cloned collection
            Random random = new Random();
            int index;
            int id = 0;
            do
            {
                index = random.Next(0,cloned.Count-1);
                this.Add(new Part(id, cloned[index].Diameter, cloned[index].Pitch, HardwareType.BOLT));
                cloned.RemoveAt(index);
                id++;
            } while (cloned.Count > 1);
            this.Add(new Part(id, cloned[0].Diameter, cloned[0].Pitch, HardwareType.BOLT));
            cloned.RemoveAt(0);
        }
    }
}

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


modified 28-Dec-20 18:13pm.

GeneralRe: Nuts and bolts - Programming contest Pin
DRHuff28-Dec-20 13:40
DRHuff28-Dec-20 13:40 
GeneralRe: Nuts and bolts - Programming contest Pin
#realJSOP28-Dec-20 20:31
mve#realJSOP28-Dec-20 20:31 
GeneralRe: Nuts and bolts - Programming contest Pin
PIEBALDconsult29-Dec-20 3:54
mvePIEBALDconsult29-Dec-20 3:54 
GeneralRe: Nuts and bolts - Programming contest Pin
#realJSOP29-Dec-20 5:00
mve#realJSOP29-Dec-20 5:00 
GeneralRe: Nuts and bolts - Programming contest Pin
PIEBALDconsult29-Dec-20 6:11
mvePIEBALDconsult29-Dec-20 6:11 
GeneralRe: Nuts and bolts - Programming contest Pin
#realJSOP30-Dec-20 3:20
mve#realJSOP30-Dec-20 3:20 
GeneralRe: Nuts and bolts - Programming contest Pin
PIEBALDconsult30-Dec-20 3:52
mvePIEBALDconsult30-Dec-20 3:52 
GeneralRe: Nuts and bolts - Programming contest Pin
Jörgen Andersson30-Dec-20 9:33
professionalJörgen Andersson30-Dec-20 9:33 
GeneralRe: Nuts and bolts - Programming contest Pin
PIEBALDconsult30-Dec-20 16:41
mvePIEBALDconsult30-Dec-20 16:41 
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 

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.