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. Friday programming quiz

Friday programming quiz

Scheduled Pinned Locked Moved The Lounge
linqcsharpdata-structuresjsonfunctional
46 Posts 24 Posters 6 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.
  • J jesarg

    I promised a programming quiz a couple weeks ago, so here's one: Given an array of integers, select the integer in the array that is the closest to the average of all integers in the array; you must use nothing but a single LINQ lambda statement. If more than one integer is the closest, then any of the closest integers will do. For example, the following code should assign "14" to the int "closest":

    int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
    int closest = // put a LINQ lambda expression here

    If you're up for actually figuring out your own solution, ignore the rest of this post. OR If you're lazy and unethical, feel free to copy-paste the sample solution in the small text below and claim it as your own. inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

    D Offline
    D Offline
    Daniel Grunwald
    wrote on last edited by
    #20

    int closest = inputs.OrderBy(i => Math.Abs(i - inputs.Average())).FirstOrDefault();

    This is O(N²) as Average() gets called for every sort key that gets calculated. We can do better by storing the average in a variable. And we can use LINQ to introduce variables within an expression:

    int closest = (from avg in new[] { inputs.Average() }
    from i in inputs
    orderby Math.Abs(i - avg)
    select i).FirstOrDefault();

    This is O(N log N), but note that instead of sorting, we just need to find the minimum. Sadly, all the Min() methods compare the element itself, not just a key. However, we can build a suitable implementation using Aggregate():

    int closest = (from avg in new[] { inputs.Average() }
    select inputs.Aggregate((a, b) => Math.Abs(a - avg) < Math.Abs(b - avg) ? a : b)
    ).Single();

    This runs in linear time, and does pretty much the same as an implementation using a loop would do.

    J A 2 Replies Last reply
    0
    • S Samuel Cragg

      Not a very efficient approach (and hope I'm not cheating, there is only one semi-colon!) but here goes:

      int closest = (from t in
      from i in inputs
      let avg = inputs.Average()
      select new { Number = i, Delta = Math.Abs(i - avg) }
      orderby t.Delta
      select t.Number).FirstOrDefault();

      J Offline
      J Offline
      jesarg
      wrote on last edited by
      #21

      It's a great solution; it's in the regular LINQ format instead of the lambda format, though. Also, it performs better than the sample solution.

      1 Reply Last reply
      0
      • D Daniel Grunwald

        int closest = inputs.OrderBy(i => Math.Abs(i - inputs.Average())).FirstOrDefault();

        This is O(N²) as Average() gets called for every sort key that gets calculated. We can do better by storing the average in a variable. And we can use LINQ to introduce variables within an expression:

        int closest = (from avg in new[] { inputs.Average() }
        from i in inputs
        orderby Math.Abs(i - avg)
        select i).FirstOrDefault();

        This is O(N log N), but note that instead of sorting, we just need to find the minimum. Sadly, all the Min() methods compare the element itself, not just a key. However, we can build a suitable implementation using Aggregate():

        int closest = (from avg in new[] { inputs.Average() }
        select inputs.Aggregate((a, b) => Math.Abs(a - avg) < Math.Abs(b - avg) ? a : b)
        ).Single();

        This runs in linear time, and does pretty much the same as an implementation using a loop would do.

        J Offline
        J Offline
        jesarg
        wrote on last edited by
        #22

        Ok, you just won the programming quiz with flying colors. Your last implementation is doing 5 million elements in sub-second. :) Thanks to everyone who participated, and hopefully, I'll be able to think of another great quiz in a week.

        1 Reply Last reply
        0
        • B BillWoodruff

          +5 Interesting challenge, even though far beyond my linquidity. Would like to take a course in Linq from you, because I feel "stymied" in my attempts to advance beyond kindergarten-level using it, assuming I am constitutionally linquidable, that is. best, Bill

          "It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle

          J Offline
          J Offline
          jesarg
          wrote on last edited by
          #23

          Thanks; LINQ becomes a lot easier once you've used it for a few months, though; you probably just need more practice rather than a course. When I started out, it was confusing, but now it feels easier than loops. LINQ almost always performs worse than using regular loops, though; typically, a loop-based implementation is about 4 times faster when you have a ton of elements.

          1 Reply Last reply
          0
          • J jesarg

            It remains sub-second time up until around 450 elements, so any test case you manually type should work fine. You think you can put together a solution that's O(N^2)?

            Y Offline
            Y Offline
            YvesDaoust
            wrote on last edited by
            #24

            This quizz is clearly O(N): computing the average is O(N); computing the absolute deviations from the average is O(N); selecting the smallest element is also O(N). I am not LINQ proficient, but I am pretty sure LINQ implements averaging in linear time, as it would do to build the deviations vector. Finding the position of the minimum of the vector is also O(N). I have nothing against high-level langages, but IMO solving such an easy problem with an O(N^3) hammer is not so welcome.

            1 Reply Last reply
            0
            • J jesarg

              I promised a programming quiz a couple weeks ago, so here's one: Given an array of integers, select the integer in the array that is the closest to the average of all integers in the array; you must use nothing but a single LINQ lambda statement. If more than one integer is the closest, then any of the closest integers will do. For example, the following code should assign "14" to the int "closest":

              int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
              int closest = // put a LINQ lambda expression here

              If you're up for actually figuring out your own solution, ignore the rest of this post. OR If you're lazy and unethical, feel free to copy-paste the sample solution in the small text below and claim it as your own. inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

              B Offline
              B Offline
              Bruno Tagliapietra
              wrote on last edited by
              #25

              maybe I've found an alternative inputs.Select(x => new { OriginalValue = x, Distance = Math.Abs(x - inputs.Average()) }).OrderBy(a => a.Distance).First().OriginalValue;

              K 1 Reply Last reply
              0
              • J jesarg

                Already replaced the avg with inputs.Average() before you posted. :) I triple-checked the post before posting it the first time, but still made that mistake; hopefully, nobody else sees it. As for the nested Lambda expression, it only has one semi-colon in it, so to me it counts as a single LINQ lambda statement. Maybe I should say "LINQ lambda statement with only one semi-colon at the end", but that would be confusing, and I'd get a ton of people asking for clarification. In any case, thank you for the feedback, and I'll be sure to refine my quiz-posting skills from all this. In the meantime, somebody should try and post their own solution; it's not much of a programming quiz without a few more solutions.

                P Offline
                P Offline
                prasun r
                wrote on last edited by
                #26

                Even if you say a lambda statement with only one semi colon, still we can have an anonymous method inside the lambda expression. So lot of clarification required in the challenge statement.

                1 Reply Last reply
                0
                • J jesarg

                  Although I don't know how the LINQ is resolved, it looks like it can't be worse than O(N^3). The Average() function costs N, the Min() costs N, the Where equals costs N, and the Select Average costs N * N. The worst nesting is N * N * N. Edited: it's at worst O(N^3).

                  K Offline
                  K Offline
                  KP Lee
                  wrote on last edited by
                  #27

                  I like Lambda expressions about as much as the help text for it is descriptive. (IE not much) I'll happily fail your quiz since I didn't sign up for your class and I'm really guessing at how this all works. In your example there were 10 items in the array. Your code:

                  inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

                  inputs.Average() loops 10 times, summing all parties and dividing by 10 to provide the average value. y in Math.Abs(y - inputs.Average()) is obviously an interation of inputs values. How it became the iteration value is because it is in the "Select" method, I think. However this expression has to be calculated 100 times (10 y's times Average's 10 loops) to find the Min() value. Math.Abs(x - inputs.Average()) will start on that 100 loop too, so that's 10,000 calculations. (I think that C# isn't smart enough to realize all these function calls on the right side produce a constant so every time the left equation is run, the right equation is re-run.) Aah, but you've put in First() so it will short-circuit the loop when 14 is found. Since 14 is the last value, that's 10,000 loops. :-D 7 Functions executed: Where (1) Math.Abs(2) Average(2) Min(1) First(1) I am never impressed with cute code. Lambda is almost always cute. 10 lines of straightforward code are more valuable than 1 line of cute. The following code

                    int avg = inputs.Average();
                    int dif1, dif2, val1;
                    val1 = inputs\[0\];
                    if (val1 > avg) dif1 = val1 - avg;
                    else dif1 = avg - val1;
                    foreach(int i in inputs) {
                        if (i > avg) dif2 = i - avg;
                        else dif2 = avg - i;
                        if (dif2 < dif1) {
                            dif1 = dif2;
                            val1 = i;
                        }
                    }
                  

                  is more verbose, but even with archane variable names, to me, this seems easier to grasp without scratching your head and going: Now, what was that again???? When it is done, you have the average, the closest value to average, and the distance from average in three variables. However, show me the lambda code to do the same thing in 2*N loops my code uses instead of N^4 loops. OK, Math.Abs is easier to read, than if/else, but I think the code cost is about the same. Go ahead and use Abs, there's no advantage in using my 2 lines of code other than showing the function is fairly easy to replicate. Average is too valuable encapsulation to re-invent that wheel. With a 400 item array, 400^4 = 2

                  P 1 Reply Last reply
                  0
                  • J jesarg

                    I promised a programming quiz a couple weeks ago, so here's one: Given an array of integers, select the integer in the array that is the closest to the average of all integers in the array; you must use nothing but a single LINQ lambda statement. If more than one integer is the closest, then any of the closest integers will do. For example, the following code should assign "14" to the int "closest":

                    int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
                    int closest = // put a LINQ lambda expression here

                    If you're up for actually figuring out your own solution, ignore the rest of this post. OR If you're lazy and unethical, feel free to copy-paste the sample solution in the small text below and claim it as your own. inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

                    S Offline
                    S Offline
                    sknake
                    wrote on last edited by
                    #28

                    I gave it a shot and came up with an answer using a few less calls:

                    int avg = inputs.OrderBy(i => Math.Abs(i - inputs.Average())).First();

                    1 Reply Last reply
                    0
                    • K KP Lee

                      I like Lambda expressions about as much as the help text for it is descriptive. (IE not much) I'll happily fail your quiz since I didn't sign up for your class and I'm really guessing at how this all works. In your example there were 10 items in the array. Your code:

                      inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

                      inputs.Average() loops 10 times, summing all parties and dividing by 10 to provide the average value. y in Math.Abs(y - inputs.Average()) is obviously an interation of inputs values. How it became the iteration value is because it is in the "Select" method, I think. However this expression has to be calculated 100 times (10 y's times Average's 10 loops) to find the Min() value. Math.Abs(x - inputs.Average()) will start on that 100 loop too, so that's 10,000 calculations. (I think that C# isn't smart enough to realize all these function calls on the right side produce a constant so every time the left equation is run, the right equation is re-run.) Aah, but you've put in First() so it will short-circuit the loop when 14 is found. Since 14 is the last value, that's 10,000 loops. :-D 7 Functions executed: Where (1) Math.Abs(2) Average(2) Min(1) First(1) I am never impressed with cute code. Lambda is almost always cute. 10 lines of straightforward code are more valuable than 1 line of cute. The following code

                        int avg = inputs.Average();
                        int dif1, dif2, val1;
                        val1 = inputs\[0\];
                        if (val1 > avg) dif1 = val1 - avg;
                        else dif1 = avg - val1;
                        foreach(int i in inputs) {
                            if (i > avg) dif2 = i - avg;
                            else dif2 = avg - i;
                            if (dif2 < dif1) {
                                dif1 = dif2;
                                val1 = i;
                            }
                        }
                      

                      is more verbose, but even with archane variable names, to me, this seems easier to grasp without scratching your head and going: Now, what was that again???? When it is done, you have the average, the closest value to average, and the distance from average in three variables. However, show me the lambda code to do the same thing in 2*N loops my code uses instead of N^4 loops. OK, Math.Abs is easier to read, than if/else, but I think the code cost is about the same. Go ahead and use Abs, there's no advantage in using my 2 lines of code other than showing the function is fairly easy to replicate. Average is too valuable encapsulation to re-invent that wheel. With a 400 item array, 400^4 = 2

                      P Offline
                      P Offline
                      PhilLenoir
                      wrote on last edited by
                      #29

                      Hear,hear! I may be an old f*rt but readability of code is paramount for maintenance. If terse and difficult to understand code is also less efficient then it fails twice. Having said that, the competition rules were set bt the creator and I'm just an aging SQL hack! :P

                      Life is like a s**t sandwich; the more bread you have, the less s**t you eat.

                      1 Reply Last reply
                      0
                      • J Johnny J

                        "We don't do homework!" AND "No programming questions in the Lounge..." ;P

                        Why can't I be applicable like John? - Me, April 2011
                        -----
                        Beidh ceol, caint agus craic againn - Seán Bán Breathnach
                        -----
                        Da mihi sis crustum Etruscum cum omnibus in eo!
                        -----
                        Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932

                        P Offline
                        P Offline
                        PhilLenoir
                        wrote on last edited by
                        #30

                        Johnny: Good sentiment, but I've only seen one alternate solution and lots of comment ... so maybe it works as a lounge post!

                        Life is like a s**t sandwich; the more bread you have, the less s**t you eat.

                        J 1 Reply Last reply
                        0
                        • P PhilLenoir

                          Johnny: Good sentiment, but I've only seen one alternate solution and lots of comment ... so maybe it works as a lounge post!

                          Life is like a s**t sandwich; the more bread you have, the less s**t you eat.

                          J Offline
                          J Offline
                          Johnny J
                          wrote on last edited by
                          #31

                          I was just pulling your leg - I have no objections to your post... ;)

                          Why can't I be applicable like John? - Me, April 2011
                          -----
                          Beidh ceol, caint agus craic againn - Seán Bán Breathnach
                          -----
                          Da mihi sis crustum Etruscum cum omnibus in eo!
                          -----
                          Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932

                          P 1 Reply Last reply
                          0
                          • J Johnny J

                            I was just pulling your leg - I have no objections to your post... ;)

                            Why can't I be applicable like John? - Me, April 2011
                            -----
                            Beidh ceol, caint agus craic againn - Seán Bán Breathnach
                            -----
                            Da mihi sis crustum Etruscum cum omnibus in eo!
                            -----
                            Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932

                            P Offline
                            P Offline
                            PhilLenoir
                            wrote on last edited by
                            #32

                            Not MY post Johnny and I was just :P

                            Life is like a s**t sandwich; the more bread you have, the less s**t you eat.

                            1 Reply Last reply
                            0
                            • J jesarg

                              I promised a programming quiz a couple weeks ago, so here's one: Given an array of integers, select the integer in the array that is the closest to the average of all integers in the array; you must use nothing but a single LINQ lambda statement. If more than one integer is the closest, then any of the closest integers will do. For example, the following code should assign "14" to the int "closest":

                              int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
                              int closest = // put a LINQ lambda expression here

                              If you're up for actually figuring out your own solution, ignore the rest of this post. OR If you're lazy and unethical, feel free to copy-paste the sample solution in the small text below and claim it as your own. inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

                              A Offline
                              A Offline
                              alanevans
                              wrote on last edited by
                              #33

                              int closest = inputs.Last(); Or have I missed the point? ;P

                              K 1 Reply Last reply
                              0
                              • D Daniel Grunwald

                                int closest = inputs.OrderBy(i => Math.Abs(i - inputs.Average())).FirstOrDefault();

                                This is O(N²) as Average() gets called for every sort key that gets calculated. We can do better by storing the average in a variable. And we can use LINQ to introduce variables within an expression:

                                int closest = (from avg in new[] { inputs.Average() }
                                from i in inputs
                                orderby Math.Abs(i - avg)
                                select i).FirstOrDefault();

                                This is O(N log N), but note that instead of sorting, we just need to find the minimum. Sadly, all the Min() methods compare the element itself, not just a key. However, we can build a suitable implementation using Aggregate():

                                int closest = (from avg in new[] { inputs.Average() }
                                select inputs.Aggregate((a, b) => Math.Abs(a - avg) < Math.Abs(b - avg) ? a : b)
                                ).Single();

                                This runs in linear time, and does pretty much the same as an implementation using a loop would do.

                                A Offline
                                A Offline
                                alanevans
                                wrote on last edited by
                                #34

                                Great answer. Can use let rather than that new []{} however: int closest = (from i in inputs
                                let avg = inputs.Average()
                                select inputs.Aggregate((a, b) => Math.Abs(a - avg) < Math.Abs(b - avg) ? a : b)
                                ).Single();
                                That is actually wrong, but this works and average is called just once:

                                        int closest = (from c in "A" //just to get it to loop once
                                                       let avg = inputs.Average()
                                                       select inputs.Aggregate((a, b) => Math.Abs(a - avg) < Math.Abs(b - avg) ? a : b)
                                          ).Single();
                                

                                Does anyone know of a nicer way to cause just one loop in linq than my 'from c in "A"' trick?

                                A 1 Reply Last reply
                                0
                                • A alanevans

                                  Great answer. Can use let rather than that new []{} however: int closest = (from i in inputs
                                  let avg = inputs.Average()
                                  select inputs.Aggregate((a, b) => Math.Abs(a - avg) < Math.Abs(b - avg) ? a : b)
                                  ).Single();
                                  That is actually wrong, but this works and average is called just once:

                                          int closest = (from c in "A" //just to get it to loop once
                                                         let avg = inputs.Average()
                                                         select inputs.Aggregate((a, b) => Math.Abs(a - avg) < Math.Abs(b - avg) ? a : b)
                                            ).Single();
                                  

                                  Does anyone know of a nicer way to cause just one loop in linq than my 'from c in "A"' trick?

                                  A Offline
                                  A Offline
                                  Adar Wesley
                                  wrote on last edited by
                                  #35

                                  This little change you suggest puts the perfomance back to O(N^2) and not linear. inputs.Average() gets called for each element in the inputs array. Daniel's solution is the correct one. --- Adar Wesley

                                  A 1 Reply Last reply
                                  0
                                  • J jesarg

                                    I promised a programming quiz a couple weeks ago, so here's one: Given an array of integers, select the integer in the array that is the closest to the average of all integers in the array; you must use nothing but a single LINQ lambda statement. If more than one integer is the closest, then any of the closest integers will do. For example, the following code should assign "14" to the int "closest":

                                    int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
                                    int closest = // put a LINQ lambda expression here

                                    If you're up for actually figuring out your own solution, ignore the rest of this post. OR If you're lazy and unethical, feel free to copy-paste the sample solution in the small text below and claim it as your own. inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

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

                                    Here's a good couple of questions to give programmers you're thinking of hiring -- if they can answer these, it's a good bet you've found yourself someone who can think a bit...: 1. In C, what does this do (and no, it doesn't matter what types the variables are as long as they're the same type)?   a^=b^=a^=b; 2. In SQL, given the following tables (relationships being obvious), write a query which returns the names of the people who play *every* sport:   People   PersonID int   PersonName varchar(50)   Sports   SportID int   SportName varchar(50)   PeopleSports   PersonID int   SportID int

                                    1 Reply Last reply
                                    0
                                    • J jesarg

                                      I promised a programming quiz a couple weeks ago, so here's one: Given an array of integers, select the integer in the array that is the closest to the average of all integers in the array; you must use nothing but a single LINQ lambda statement. If more than one integer is the closest, then any of the closest integers will do. For example, the following code should assign "14" to the int "closest":

                                      int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
                                      int closest = // put a LINQ lambda expression here

                                      If you're up for actually figuring out your own solution, ignore the rest of this post. OR If you're lazy and unethical, feel free to copy-paste the sample solution in the small text below and claim it as your own. inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

                                      R Offline
                                      R Offline
                                      Robbie Couret
                                      wrote on last edited by
                                      #37

                                      Could be simplified to:

                                      int closest = inputs.First(x => Math.Abs(x - inputs.Average()) == inputs.Min(y => Math.Abs(y - inputs.Average())));

                                      but I don't like to perform the same calculation twice, so I'd be more comfortable with:

                                      double avg = inputs.Average();
                                      int closest = inputs.First(x => Math.Abs(x - avg) == inputs.Min(y => Math.Abs(y - avg)));

                                      1 Reply Last reply
                                      0
                                      • J jesarg

                                        I promised a programming quiz a couple weeks ago, so here's one: Given an array of integers, select the integer in the array that is the closest to the average of all integers in the array; you must use nothing but a single LINQ lambda statement. If more than one integer is the closest, then any of the closest integers will do. For example, the following code should assign "14" to the int "closest":

                                        int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
                                        int closest = // put a LINQ lambda expression here

                                        If you're up for actually figuring out your own solution, ignore the rest of this post. OR If you're lazy and unethical, feel free to copy-paste the sample solution in the small text below and claim it as your own. inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

                                        J Offline
                                        J Offline
                                        John Petrak2
                                        wrote on last edited by
                                        #38

                                        int closest = inputs.OrderBy(x => Math.Abs(inputs.Average() - x)).First();

                                        1 Reply Last reply
                                        0
                                        • J jesarg

                                          I promised a programming quiz a couple weeks ago, so here's one: Given an array of integers, select the integer in the array that is the closest to the average of all integers in the array; you must use nothing but a single LINQ lambda statement. If more than one integer is the closest, then any of the closest integers will do. For example, the following code should assign "14" to the int "closest":

                                          int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
                                          int closest = // put a LINQ lambda expression here

                                          If you're up for actually figuring out your own solution, ignore the rest of this post. OR If you're lazy and unethical, feel free to copy-paste the sample solution in the small text below and claim it as your own. inputs.Where(x => Math.Abs(x - inputs.Average()) == inputs.Select(y => Math.Abs(y - inputs.Average())).Min()).First();

                                          H Offline
                                          H Offline
                                          Harley L Pebley
                                          wrote on last edited by
                                          #39

                                          Here are two alternate implementations. Two might be cheating a bit, but it satisfies the "only one semi-colon" rule. :-) As others have pointed out, the answer you gave is pretty slow. To those who say LINQ can't be fast, algorithm choice makes a huge difference. I ran tests for the three implementations over the original input set, the values 1-99 and -499 to 499. To help average out timing inconsistencies, the tests were run 10,000 times. Values are in ms.

                                          Quiz inputs

                                          100 items

                                          1000 items

                                          Quiz solution

                                          284

                                          84,573

                                          Unknown. I stopped it after about 6 hours.

                                          Alternate 1

                                          80

                                          1,945

                                          167,099

                                          Alternate 2

                                          75

                                          479

                                          7,246

                                          Alternate 1

                                          int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
                                          int closest = inputs
                                          .Select(i => new {val = i, dist = Math.Abs(i - inputs.Average())})
                                          .OrderBy(a => a.dist)
                                          .First().val;

                                          Alternate 2

                                          int[] inputs = {1, 2, 3, 5, -1, 7, 145, -33, 22, 14};
                                          int closest = (from x in new []{1}
                                          let avg = inputs.Average()
                                          from i in inputs
                                          select new { val = i, dist = Math.Abs(i-avg)})
                                          .OrderBy(a => a.dist)
                                          .First().val

                                          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