locked
Unable to write Calculate using Linq RRS feed

  • Question

  • User-1256377279 posted

    Hi All,

    Please can you help to calculate below request using LINQ

    1. data structure to represent a customer, with the following fields. The coordinates represent a grid location on a 100 x 100 map, with all the units being kilometres.
      • Name (the customer’s name)
      • X (the X grid coordinate on a map as an integer value)
      • Y (the Y grid coordinate on a map as an integer value)
    2. Customer Name

      X

      Y

      Customer 1

      10

      20

      Customer 2

      90

      24

      Customer 3

      34

      63

      Customer 4

      67

      1

      Customer 5

      24

      84

      Customer 6

      51

      44

      Customer 7

      97

      92

      Customer 8

      77

      13

      Customer 9

      35

      39

      Customer 10

      85

      29

    Write a function that takes two input parameters, the starting X and Y coordinates. The function should calculate the shortest route that it takes to visit all ten customers, starting at the initial coordinates passed in to the function. Any distance calculations used in your algorithm should ignore fractional values and integer arithmetic should be used throughout

    Many Thanks,

    SHabbir

    Monday, March 29, 2021 9:57 PM

Answers

  • User1535942433 posted

    Hi shabbir_215,

    I have created a demo that you could get the customer's name which have the shortest route.Just like this:

    static void Main(string[] args)
            {
                List<P> pxy = new List<P>
                {
                    new P{ Name="Customer1", X=10,Y=20},
                    new P{ Name="Customer2",X=90,Y=24},
                    new P{ Name="Customer3",X=34,Y=63},
                    new P{ Name="Customer4",X=67,Y=1},
                    new P{ Name="Customer5",X=24,Y=84},
                    new P{ Name="Customer6",X=51,Y=44},
                    new P{ Name="Customer7",X=97,Y=92},
                    new P{ Name="Customer8",X=77,Y=13},
                    new P{ Name="Customer9",X=35,Y=39},
                    new P{ Name="Customer10",X=85,Y=29}
                };
                Console.WriteLine("Please enter X");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("Please enter Y");
                int y = int.Parse(Console.ReadLine());
    
                var closestToOrigin = pxy
        .Select(p => new { P = p, Distance2 = Math.Sqrt((x-p.X)*(x-p.X)+(y-p.Y)*(y-p.Y)) })
        .Aggregate((p1, p2) => p1.Distance2 < p2.Distance2 ? p1 : p2)
        .P;
    
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2021 6:57 AM
  • User-1256377279 posted

    Hi shabbir_215,

    I have created a demo that you could get the customer's name which have the shortest route.Just like this:

    static void Main(string[] args)
            {
                List<P> pxy = new List<P>
                {
                    new P{ Name="Customer1", X=10,Y=20},
                    new P{ Name="Customer2",X=90,Y=24},
                    new P{ Name="Customer3",X=34,Y=63},
                    new P{ Name="Customer4",X=67,Y=1},
                    new P{ Name="Customer5",X=24,Y=84},
                    new P{ Name="Customer6",X=51,Y=44},
                    new P{ Name="Customer7",X=97,Y=92},
                    new P{ Name="Customer8",X=77,Y=13},
                    new P{ Name="Customer9",X=35,Y=39},
                    new P{ Name="Customer10",X=85,Y=29}
                };
                Console.WriteLine("Please enter X");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("Please enter Y");
                int y = int.Parse(Console.ReadLine());
    
                var closestToOrigin = pxy
        .Select(p => new { P = p, Distance2 = Math.Sqrt((x-p.X)*(x-p.X)+(y-p.Y)*(y-p.Y)) })
        .Aggregate((p1, p2) => p1.Distance2 < p2.Distance2 ? p1 : p2)
        .P;
    
            }

    Best regards,

    Yijing Sun

    Thank You so much Yijing Sun, it worked perfect.

    I owe you a cup of coffee

    Thanks Again

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2021 7:50 AM

All replies

  • User475983607 posted

    Come on man, really?   It sounds like homework or an interview question.  Either way, I'm sure the entity that is asking you to solve this problem wants you're work not someone on the Internet.    

    Monday, March 29, 2021 10:08 PM
  • User-1256377279 posted

    mgebhard

    Come on man, really?   It sounds like homework or an interview question.  Either way, I'm sure the entity that is asking you to solve this problem wants you're work not someone on the Internet.    

    thanks for your kind Advice, I had already tried so thought of sharing to all so they might have better solution.

    but that’s was really rude reply

    Tuesday, March 30, 2021 6:02 AM
  • User1535942433 posted

    Hi shabbir_215,

    I have created a demo that you could get the customer's name which have the shortest route.Just like this:

    static void Main(string[] args)
            {
                List<P> pxy = new List<P>
                {
                    new P{ Name="Customer1", X=10,Y=20},
                    new P{ Name="Customer2",X=90,Y=24},
                    new P{ Name="Customer3",X=34,Y=63},
                    new P{ Name="Customer4",X=67,Y=1},
                    new P{ Name="Customer5",X=24,Y=84},
                    new P{ Name="Customer6",X=51,Y=44},
                    new P{ Name="Customer7",X=97,Y=92},
                    new P{ Name="Customer8",X=77,Y=13},
                    new P{ Name="Customer9",X=35,Y=39},
                    new P{ Name="Customer10",X=85,Y=29}
                };
                Console.WriteLine("Please enter X");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("Please enter Y");
                int y = int.Parse(Console.ReadLine());
    
                var closestToOrigin = pxy
        .Select(p => new { P = p, Distance2 = Math.Sqrt((x-p.X)*(x-p.X)+(y-p.Y)*(y-p.Y)) })
        .Aggregate((p1, p2) => p1.Distance2 < p2.Distance2 ? p1 : p2)
        .P;
    
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2021 6:57 AM
  • User-1256377279 posted

    Hi shabbir_215,

    I have created a demo that you could get the customer's name which have the shortest route.Just like this:

    static void Main(string[] args)
            {
                List<P> pxy = new List<P>
                {
                    new P{ Name="Customer1", X=10,Y=20},
                    new P{ Name="Customer2",X=90,Y=24},
                    new P{ Name="Customer3",X=34,Y=63},
                    new P{ Name="Customer4",X=67,Y=1},
                    new P{ Name="Customer5",X=24,Y=84},
                    new P{ Name="Customer6",X=51,Y=44},
                    new P{ Name="Customer7",X=97,Y=92},
                    new P{ Name="Customer8",X=77,Y=13},
                    new P{ Name="Customer9",X=35,Y=39},
                    new P{ Name="Customer10",X=85,Y=29}
                };
                Console.WriteLine("Please enter X");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("Please enter Y");
                int y = int.Parse(Console.ReadLine());
    
                var closestToOrigin = pxy
        .Select(p => new { P = p, Distance2 = Math.Sqrt((x-p.X)*(x-p.X)+(y-p.Y)*(y-p.Y)) })
        .Aggregate((p1, p2) => p1.Distance2 < p2.Distance2 ? p1 : p2)
        .P;
    
            }

    Best regards,

    Yijing Sun

    Thank You so much Yijing Sun, it worked perfect.

    I owe you a cup of coffee

    Thanks Again

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2021 7:50 AM