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.
  • 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
                • 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
                  #38

                  Seems 'sort and take the first N' is the consensus on the algorithm, I think everyone's code does the same thing.

                  1 Reply Last reply
                  0
                  • C Chris Maunder

                    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 Offline
                    S Offline
                    S Houghtelin
                    wrote on last edited by
                    #39

                    Chris Maunder wrote:

                    The hamsters involved are currently taking some time off to spend more time with their families.

                    How did they escape? You forgot to lock the door again... :D

                    It was broke, so I fixed it.

                    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

                      J Offline
                      J Offline
                      Jason Hooper
                      wrote on last edited by
                      #40

                      Does the language have to already exist?

                      Jason

                      B 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

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #41

                        "GIMMEE CODEZ!!!" in Q&A should do it...

                        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        1 Reply Last reply
                        0
                        • J Jason Hooper

                          Does the language have to already exist?

                          Jason

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

                          Well, if not, it is obviously possible to invent a language where one symbol does this operation :P. Since "sort and take" seems to be how to do it, it would be easy to conceive of a language where putting two symbols for "take" and "sort" next to each other would create a composite function that did it, making two characters the sensible theoretical minimum.

                          P J D 3 Replies Last reply
                          0
                          • C Chris Maunder

                            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 Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #43

                            Chris Maunder wrote:

                            How does SQL handle them?

                            You tell me how you want them handled and I'll tell SQL how. It's SQL, not LISP.

                            1 Reply Last reply
                            0
                            • B BobJanova

                              Well, if not, it is obviously possible to invent a language where one symbol does this operation :P. Since "sort and take" seems to be how to do it, it would be easy to conceive of a language where putting two symbols for "take" and "sort" next to each other would create a composite function that did it, making two characters the sensible theoretical minimum.

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

                              f(x)

                              1 Reply Last reply
                              0
                              • B BobJanova

                                Well, if not, it is obviously possible to invent a language where one symbol does this operation :P. Since "sort and take" seems to be how to do it, it would be easy to conceive of a language where putting two symbols for "take" and "sort" next to each other would create a composite function that did it, making two characters the sensible theoretical minimum.

                                J Offline
                                J Offline
                                Jason Hooper
                                wrote on last edited by
                                #45

                                Yeah exactly. Since I'm the first (i think) to propose it but I don't feel like spending 5 hours creating an interpreter, let's just pretend I'm the winner.

                                Jason

                                B 1 Reply Last reply
                                0
                                • J Jason Hooper

                                  Yeah exactly. Since I'm the first (i think) to propose it but I don't feel like spending 5 hours creating an interpreter, let's just pretend I'm the winner.

                                  Jason

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

                                  I could easily modify my Rowan interpreter (what I posted my solution in) to implement sort as a single symbol, but it isn't worth it just for something like this :-\ . I don't allow composition of functions by juxtaposition like that though because it's confusing (see J).

                                  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
                                    Rajesh R Subramanian
                                    wrote on last edited by
                                    #47

                                    std::list li; //A list, so that it can be sorted in ascending order.
                                    int i = 0, /*i is increment counter*/ n = 3; //n denotes how many small numbers to print.
                                    while(i++<5) li.push_back(i*3); //fill the list with dummy data.
                                    li.sort(); //Small numbers will go to the end in ascending order.
                                    std::list::iterator it = li.begin();

                                    **while(n-- && it!=li.end()) std::cout << \*(it++) << std::endl;** //This is the line you're looking for. :)
                                    

                                    "Real men drive manual transmission" - Rajesh.

                                    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

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

                                      X| If we're allowed to cheat by using third party software, I'd 1. Copy and paste to Minitab 2. Click on Graphs/Stem and Leaf The top line of numbers in the displayed graph are the smallest values in the set, ordered ascending. :-D

                                      Will Rogers never met me.

                                      1 Reply Last reply
                                      0
                                      • B BobJanova

                                        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 Offline
                                        G Offline
                                        Gary Wheeler
                                        wrote on last edited by
                                        #49

                                        You win the "We Can't Judge Your Submission Because It's Incomprehensible" award.

                                        Software Zen: delete this;

                                        B 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

                                          P Offline
                                          P Offline
                                          parths
                                          wrote on last edited by
                                          #50

                                          void find_n_in_m(int *mData, int *nData, unsigned int m, unsigned int n)
                                          {
                                          unsigned int ctr, ctr2 = 0;
                                          for(unsigned int i = 0; i < n; i++)
                                          nData[i] = mData[i];
                                          m--;
                                          while (((ctr = m) && (m >= n))
                                          && ((mData[ctr] >= nData[ctr2]) ?
                                          ((++ctr2 < n) || (ctr2 = 0) || m--) :
                                          (((mData[ctr] ^= nData[ctr2]) && (nData[ctr2] ^= mData[ctr]) && (mData[ctr] ^= nData[ctr2]) && (ctr2 = 0)) || 1)
                                          ));
                                          }

                                          - Doesn't give a sorted list - Modifies the input array (nothing mentioned against that in the specification but that can be avoided by adding an input argument for an temp / scratch buffer of the same size as the source buffer) - Tried it on VS2008 Express Ed and with a few basic data so I don't know if it's correct for all inputs. - What it does is fill the destination with the first n elements then goes about trying to see if it can place the elements from n to m into the new array. Thought I'd post it while I'm trying to improve ( :~ ) it.

                                          "It was when I found out I could make mistakes that I knew I was on to something." -Ornette Coleman "Philosophy is a study that lets us be unhappy more intelligently." -Anon.

                                          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