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's Coding Challenge

Friday's Coding Challenge

Scheduled Pinned Locked Moved The Lounge
c++architectureperformancehelplounge
76 Posts 35 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.
  • C Chris Maunder

    What's the smallest code you can come up with to find the n smallest numbers in a random sample of m numbers where n < m. Any language, speed is not an issue.

    cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

    X Offline
    X Offline
    Xiangyang Liu
    wrote on last edited by
    #2

    Are we talking about integers?

    My Younger Son & His "PET"

    C realJSOPR 2 Replies Last reply
    0
    • C Chris Maunder

      What's the smallest code you can come up with to find the n smallest numbers in a random sample of m numbers where n < m. Any language, speed is not an issue.

      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

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

      SELECT TOP n FROM Sample ORDER BY Value Edit: How should we handle duplicates?

      C K 2 Replies Last reply
      0
      • C Chris Maunder

        What's the smallest code you can come up with to find the n smallest numbers in a random sample of m numbers where n < m. Any language, speed is not an issue.

        cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

        B Offline
        B Offline
        Bassam Abdul Baki
        wrote on last edited by
        #4

        In English, select the first n numbers, sort them, then check the next m - n numbers to compare with the largest n number. Delete the old nth number and insert the new one in the right order.

        Web - BM - RSS - Math - LinkedIn

        C 1 Reply Last reply
        0
        • B Bassam Abdul Baki

          In English, select the first n numbers, sort them, then check the next m - n numbers to compare with the largest n number. Delete the old nth number and insert the new one in the right order.

          Web - BM - RSS - Math - LinkedIn

          C Offline
          C Offline
          Chris Maunder
          wrote on last edited by
          #5

          The funny thing was that when I drafted the question I was thinking about all the smart comments that would be made and so I added "programming" before the word "language". But then thought: Nah - no one would answer with an English sentence. :doh:

          cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

          B S 2 Replies Last reply
          0
          • X Xiangyang Liu

            Are we talking about integers?

            My Younger Son & His "PET"

            C Offline
            C Offline
            Chris Maunder
            wrote on last edited by
            #6

            Not necessarily, but sure, if you want to restrict it to ints.

            cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

            1 Reply Last reply
            0
            • P PIEBALDconsult

              SELECT TOP n FROM Sample ORDER BY Value Edit: How should we handle duplicates?

              C Offline
              C Offline
              Chris Maunder
              wrote on last edited by
              #7

              You took the easy one ;)

              PIEBALDconsult wrote:

              How should we handle duplicates

              Be creative. How does SQL handle them?

              cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

              P 1 Reply Last reply
              0
              • C Chris Maunder

                The funny thing was that when I drafted the question I was thinking about all the smart comments that would be made and so I added "programming" before the word "language". But then thought: Nah - no one would answer with an English sentence. :doh:

                cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                B Offline
                B Offline
                Bassam Abdul Baki
                wrote on last edited by
                #8

                Would you prefer an answer in Excel? :D

                Web - BM - RSS - Math - LinkedIn

                D 1 Reply Last reply
                0
                • C Chris Maunder

                  What's the smallest code you can come up with to find the n smallest numbers in a random sample of m numbers where n < m. Any language, speed is not an issue.

                  cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #9

                  Assuming sample is an array of numbers:

                  int smallest = Array.Sort(sample)[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
                  -----
                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                  C 1 Reply Last reply
                  0
                  • C Chris Maunder

                    What's the smallest code you can come up with to find the n smallest numbers in a random sample of m numbers where n < m. Any language, speed is not an issue.

                    cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                    B Offline
                    B Offline
                    BobJanova
                    wrote on last edited by
                    #10

                    APL:

                    f←{⍺↑⍵⌷⍨⍋⍵}

                    call like

                    n f (sample vector)

                    eg

                    f←{⍺↑⍵⌷⍨⍋⍵}
                    {f}
                    xx←20?30 // 20 different ints in 1-30
                    (23 28 14 12 10 8 15 3 2 7 26 4 20 29 24 30 25 18 21 27)
                    10 f xx // smallest 10 values in xx
                    (2 3 4 7 8 10 12 14 15 18)

                    This is in my personal dialect since I don't have a licensed major APL on this machine, but the function is essentially the same in normal variants.

                    G L 2 Replies Last reply
                    0
                    • C Chris Maunder

                      What's the smallest code you can come up with to find the n smallest numbers in a random sample of m numbers where n < m. Any language, speed is not an issue.

                      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                      E Offline
                      E Offline
                      Ennis Ray Lynch Jr
                      wrote on last edited by
                      #11

                      Smallest in source lines or smallest in compiled output?

                      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                      C 1 Reply Last reply
                      0
                      • C Chris Maunder

                        What's the smallest code you can come up with to find the n smallest numbers in a random sample of m numbers where n < m. Any language, speed is not an issue.

                        cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                        S Offline
                        S Offline
                        Simon_Whale
                        wrote on last edited by
                        #12

                        I have assumed a list of integers

                        List Numbers = new List();
                        Random r = new Random(10);

                        for (int i = 0; i < 10; i++)
                        {
                        Numbers.Add(r.Next(0, 100));
                        }

                        Numbers.Sort();
                        var Values = Number.Take(5);

                        Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

                        C 1 Reply Last reply
                        0
                        • X Xiangyang Liu

                          Are we talking about integers?

                          My Younger Son & His "PET"

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #13

                          Based on the way the task was phrased, I think it's safe to assume we're talking numeric values, but the intrinsic type is of no real consequence in the actual mechanics of the task.

                          ".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
                          -----
                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                          1 Reply Last reply
                          0
                          • C Chris Maunder

                            What's the smallest code you can come up with to find the n smallest numbers in a random sample of m numbers where n < m. Any language, speed is not an issue.

                            cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                            N Offline
                            N Offline
                            Nagy Vilmos
                            wrote on last edited by
                            #14

                            int[] smallest (int[] args, int count) {
                            int[] copy = java.util.Arrays.copyOf(args,args.length);
                            java.util.Arrays.sort(copy);
                            return java.util.Arrays.copyOf(args, count);
                            }

                            If you don't need it to preserve the original order, then you can drop the first line. This is safe for count > args.length.


                            Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                            1 Reply Last reply
                            0
                            • E Ennis Ray Lynch Jr

                              Smallest in source lines or smallest in compiled output?

                              Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                              C Offline
                              C Offline
                              Chris Maunder
                              wrote on last edited by
                              #15

                              Source.

                              cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                              1 Reply Last reply
                              0
                              • realJSOPR realJSOP

                                Assuming sample is an array of numbers:

                                int smallest = Array.Sort(sample)[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
                                -----
                                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                                C Offline
                                C Offline
                                Chris Maunder
                                wrote on last edited by
                                #16

                                You may want to read the specs again.

                                cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                realJSOPR 2 Replies Last reply
                                0
                                • S Simon_Whale

                                  I have assumed a list of integers

                                  List Numbers = new List();
                                  Random r = new Random(10);

                                  for (int i = 0; i < 10; i++)
                                  {
                                  Numbers.Add(r.Next(0, 100));
                                  }

                                  Numbers.Sort();
                                  var Values = Number.Take(5);

                                  Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

                                  C Offline
                                  C Offline
                                  Chris Maunder
                                  wrote on last edited by
                                  #17

                                  That's 1 line too many ;)

                                  cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                  S 1 Reply Last reply
                                  0
                                  • C Chris Maunder

                                    The funny thing was that when I drafted the question I was thinking about all the smart comments that would be made and so I added "programming" before the word "language". But then thought: Nah - no one would answer with an English sentence. :doh:

                                    cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

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

                                    Chris Maunder wrote:

                                    I was thinking about all the smart comments that would be made

                                    Well Chris, you remember how the last challenge went. :)

                                    "the meat from that butcher is just the dogs danglies, absolutely amazing cuts of beef." - DaveAuld (2011)
                                    "No, that is just the earthly manifestation of the Great God Retardon." - Nagy Vilmos (2011) "It is the celestial scrotum of good luck!" - Nagy Vilmos (2011)

                                    C 1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      What's the smallest code you can come up with to find the n smallest numbers in a random sample of m numbers where n < m. Any language, speed is not an issue.

                                      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                      D Offline
                                      D Offline
                                      Dalek Dave
                                      wrote on last edited by
                                      #19

                                      As an accountant I would suggest...

                                      Range("A:A").Select
                                      ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
                                      ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                                      With ActiveWorkbook.Worksheets("Sheet1").Sort
                                          .SetRange Range("A:A")
                                          .Header = xlNo
                                          .MatchCase = False
                                          .Orientation = xlTopToBottom
                                          .SortMethod = xlPinYin
                                          .Apply
                                      End With
                                      Range("C1").Select
                                      ActiveCell.FormulaR1C1 = "=RC\[-2\]"
                                      Range("C2").Select
                                      ActiveCell.FormulaR1C1 = "=COUNTIF(R\[-1\]C\[-2\]:R\[17\]C\[-2\],R\[-1\]C)"
                                      Range("C3").Select
                                      

                                      That that is accountants all over!

                                      --------------------------------- I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] English League Tables - Live

                                      C N N R 4 Replies Last reply
                                      0
                                      • D Dalek Dave

                                        As an accountant I would suggest...

                                        Range("A:A").Select
                                        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
                                        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                                        With ActiveWorkbook.Worksheets("Sheet1").Sort
                                            .SetRange Range("A:A")
                                            .Header = xlNo
                                            .MatchCase = False
                                            .Orientation = xlTopToBottom
                                            .SortMethod = xlPinYin
                                            .Apply
                                        End With
                                        Range("C1").Select
                                        ActiveCell.FormulaR1C1 = "=RC\[-2\]"
                                        Range("C2").Select
                                        ActiveCell.FormulaR1C1 = "=COUNTIF(R\[-1\]C\[-2\]:R\[17\]C\[-2\],R\[-1\]C)"
                                        Range("C3").Select
                                        

                                        That that is accountants all over!

                                        --------------------------------- I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] English League Tables - Live

                                        N Offline
                                        N Offline
                                        Nagy Vilmos
                                        wrote on last edited by
                                        #20

                                        That's uglier than my photo!


                                        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                                        1 Reply Last reply
                                        0
                                        • D Dalek Dave

                                          As an accountant I would suggest...

                                          Range("A:A").Select
                                          ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
                                          ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                                          With ActiveWorkbook.Worksheets("Sheet1").Sort
                                              .SetRange Range("A:A")
                                              .Header = xlNo
                                              .MatchCase = False
                                              .Orientation = xlTopToBottom
                                              .SortMethod = xlPinYin
                                              .Apply
                                          End With
                                          Range("C1").Select
                                          ActiveCell.FormulaR1C1 = "=RC\[-2\]"
                                          Range("C2").Select
                                          ActiveCell.FormulaR1C1 = "=COUNTIF(R\[-1\]C\[-2\]:R\[17\]C\[-2\],R\[-1\]C)"
                                          Range("C3").Select
                                          

                                          That that is accountants all over!

                                          --------------------------------- I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC Link[^] English League Tables - Live

                                          C Offline
                                          C Offline
                                          Chris Maunder
                                          wrote on last edited by
                                          #21

                                          My eyes! They burn!

                                          cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                          D 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