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

    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

      N C 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

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

        My eyes! They burn!

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

        D 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

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

          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
          • S Slacker007

            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 Offline
            C Offline
            Chris Maunder
            wrote on last edited by
            #22

            Yes, like the weekly surveys, they are merely pedant fodder. :)

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

            D 1 Reply Last reply
            0
            • B Bassam Abdul Baki

              Would you prefer an answer in Excel? :D

              Web - BM - RSS - Math - LinkedIn

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

              I took you at your word...See Below :)

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

              1 Reply Last reply
              0
              • C Chris Maunder

                My eyes! They burn!

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

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

                Functional but ugly. I have dated some like that!

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

                1 Reply Last reply
                0
                • C Chris Maunder

                  Yes, like the weekly surveys, they are merely pedant fodder. :)

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

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

                  If you don't feed the pedants, they will revolt!

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

                  P 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

                    W Offline
                    W Offline
                    wizardzz
                    wrote on last edited by
                    #26

                    Elephanting Sunshine! You expect us to do your homework?

                    "I have a theory that the truth is never told during the nine-to-five hours. " — Hunter S. Thompson

                    1 Reply Last reply
                    0
                    • C Chris Maunder

                      You may want to read the specs again.

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

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

                      I assumed n was 1. :)

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

                        R Offline
                        R Offline
                        Roger Wright
                        wrote on last edited by
                        #28

                        Is this somehow related to the curious error page I received this morning from Web04? The one featuring ravenous hamsters obviously recovering from a three day methamphetamine/ethanol binge?

                        Will Rogers never met me.

                        C 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

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

                          In English (again): :D Assume m = A*n + B. If m >> n, then it might be easier to first sort each of the A n-segments and B segment separately. Compare the first element of the A-1 lists and B to the last element of the first A list (or to the last element of the smallest starting A-list) and eliminate all the A segments that are greater than that. Then repeat the process again using n elements selected vertically.

                          Web - BM - RSS - Math - LinkedIn

                          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

                            N Offline
                            N Offline
                            NormDroid
                            wrote on last edited by
                            #30

                            Help my eyes, my poor eyes are bleeding :)

                            Software Kinetics Wear a hard hat it's under construction
                            Metro RSS

                            1 Reply Last reply
                            0
                            • D Dalek Dave

                              If you don't feed the pedants, they will revolt!

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

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

                              "The pedants are revolting!" (a la The Wizard of Id)

                              1 Reply Last reply
                              0
                              • R Roger Wright

                                Is this somehow related to the curious error page I received this morning from Web04? The one featuring ravenous hamsters obviously recovering from a three day methamphetamine/ethanol binge?

                                Will Rogers never met me.

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

                                The hamsters have asked for an end to the rumours and baseless allegations of alleged behaviour during certain incidents. The hamsters involved are currently taking some time off to spend more time with their families.

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

                                S R 2 Replies Last reply
                                0
                                • R Roger Wright

                                  Is this somehow related to the curious error page I received this morning from Web04? The one featuring ravenous hamsters obviously recovering from a three day methamphetamine/ethanol binge?

                                  Will Rogers never met me.

                                  L Offline
                                  L Offline
                                  leppie
                                  wrote on last edited by
                                  #33

                                  Roger Wright wrote:

                                  The one featuring ravenous hamsters obviously recovering from a three day methamphetamine/ethanol binge?

                                  You read my mind ;p

                                  IronScheme
                                  ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                                  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

                                    M Offline
                                    M Offline
                                    Mel Padden
                                    wrote on last edited by
                                    #34

                                    Try this:

                                    #generation of the list
                                    import random
                                    sample = []; n=10; m = 100;rnd = random.Random();
                                    for it in range (1, m): sample.append(rnd.randint(1,n*m))

                                    #then
                                    sample.sort()
                                    print sample[:n]
                                    #boom

                                    @Chris - When are you planning to start supporting Python in the forum syntax highlighter? :-O [Edit] Not that I'm claiming that my little snippet is beautiful; far from it. I'm still way off being a full-blown Pythonista. But it sure would be nice to have it supported.

                                    Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. In the face of ambiguity, refuse the temptation to guess.

                                    1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      That's 1 line too many ;)

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

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

                                      My only other alternative is using this List.Sort overloaded method[^] but it would involve a more complex object and writing your own Compare method Scrap that idea Leppie has a great answer in my opinion[^]

                                      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

                                      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

                                        L Offline
                                        L Offline
                                        leppie
                                        wrote on last edited by
                                        #36

                                        sample.OrderBy(x => x).Take(n)

                                        IronScheme
                                        ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                                        1 Reply Last reply
                                        0
                                        • C Chris Maunder

                                          You may want to read the specs again.

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

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

                                          //assumptions
                                          // n = 2
                                          int[] sample = new int[]{8,1,9,3,2};
                                          int[] sample2 = new int[2];

                                          //code
                                          Array.Sort(sample);
                                          sample2 = sample.Take(sample2.Length).ToArray();

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