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 (It's back) [modified]

Friday Programming Quiz (It's back) [modified]

Scheduled Pinned Locked Moved The Lounge
htmlcomtutorial
38 Posts 13 Posters 0 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.
  • R Rama Krishna Vavilala

    Back on Popular demand You are given two lists. List 1 contains certain strings in a particular order. List 2 contains all the allowed values in List 1. List 1, however, can contain some elements not in List 2. The objective is to generate a List 3 which will have elements from List 1 in exactly the same order specified in List 1 followed by elements not in List 1 but present in List 2. Any elements not in List 2 should not be included. Example: List 1:

    A,B,C,D

    List 2:

    B,A,X,S,L,D

    Output:

    A,B,D,X,S,L


    Last modified: 10mins after originally posted --

    Proud to be a CPHog user

    S Offline
    S Offline
    Shog9 0
    wrote on last edited by
    #15

    var list1 = ['A','B','C','D'];
    var list2 = ['B','A','X','S','L','D'];

    var result = list1.filter(function(i) list2.indexOf(i) >= 0)
    .concat( list2.filter(function(i) list1.indexOf(i) < 0 ) );

    Javascript 1.8 (tested on Firefox 3.0.1)

    You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...

    N 1 Reply Last reply
    0
    • E Ennis Ray Lynch Jr

      That is the efficiency of the algorithm, as in O(n^2) or less. It is a large topic that I cannot explain here.

      Need a C# Consultant? I'm available.
      Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

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

      Ennis Ray Lynch, Jr. wrote:

      That is the efficiency of the algorithm, as in O(n^2) or less. It is a large topic that I cannot explain here.

      Jeez. I hope you being sarcastic... :| What is n, the size of the input? Size of the board?

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      E P 2 Replies Last reply
      0
      • L leppie

        Ennis Ray Lynch, Jr. wrote:

        That is the efficiency of the algorithm, as in O(n^2) or less. It is a large topic that I cannot explain here.

        Jeez. I hope you being sarcastic... :| What is n, the size of the input? Size of the board?

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 alpha 4a out now (29 May 2008)

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

        your kidding right?

        Need a C# Consultant? I'm available.
        Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

        L 1 Reply Last reply
        0
        • L leppie

          Ennis Ray Lynch, Jr. wrote:

          That is the efficiency of the algorithm, as in O(n^2) or less. It is a large topic that I cannot explain here.

          Jeez. I hope you being sarcastic... :| What is n, the size of the input? Size of the board?

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #18

          :eek:

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

          1 Reply Last reply
          0
          • J Judah Gabriel Himango

            Sweet! I love these! Is it cheating to use LINQ?

            var list1Allowed = from element in list1
                               where list2.Contains(element)
                               select element;

            var list2ElementsNotInList1 = from element in list2
                                          where !list1.Contains(element)
                                         select element;

            var result = list1Allowed.Concat(list2ElementsNotInList1);

            Alternately, since we're not doing any fabulous let clauses, we could just call the extension methods directly for a more terse syntax:

            var list1Allowed = list1.Where(el => list2.Contains(el));
            var list2ElementsNotInList1 = list2.Where(el => !list1.Contains(el));
            var result = list1Allowed.Concat(list2ElementsNotInList1);

            Nifty! I :heart: LINQ. :)

            Life, family, faith: Give me a visit. From my latest post: "The themes and truths of the Jewish holidays follow God's complete plan for this world. They are the root from which Christianity sprang and the historical reasons the church had for leaving them behind were unsound." Judah Himango

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #19

            Judah Himango wrote:

            var list2ElementsNotInList1 = list2.Where(el => !list1.Contains(el));

            Hey Judah, I would probably replace that with :

            var list2ElementsNotInList1 = list2.Except(list1);

            Regards, Nish


            Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
            My latest book : C++/CLI in Action / Amazon.com link

            1 Reply Last reply
            0
            • J Judah Gabriel Himango

              Sweet! I love these! Is it cheating to use LINQ?

              var list1Allowed = from element in list1
                                 where list2.Contains(element)
                                 select element;

              var list2ElementsNotInList1 = from element in list2
                                            where !list1.Contains(element)
                                           select element;

              var result = list1Allowed.Concat(list2ElementsNotInList1);

              Alternately, since we're not doing any fabulous let clauses, we could just call the extension methods directly for a more terse syntax:

              var list1Allowed = list1.Where(el => list2.Contains(el));
              var list2ElementsNotInList1 = list2.Where(el => !list1.Contains(el));
              var result = list1Allowed.Concat(list2ElementsNotInList1);

              Nifty! I :heart: LINQ. :)

              Life, family, faith: Give me a visit. From my latest post: "The themes and truths of the Jewish holidays follow God's complete plan for this world. They are the root from which Christianity sprang and the historical reasons the church had for leaving them behind were unsound." Judah Himango

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #20

              In fact this would be even more straightforward :

              var result = list1.Intersect(list2).Concat(list2.Except(list1));

              Regards, Nish


              Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
              My latest book : C++/CLI in Action / Amazon.com link

              S J 2 Replies Last reply
              0
              • N Nish Nishant

                In fact this would be even more straightforward :

                var result = list1.Intersect(list2).Concat(list2.Except(list1));

                Regards, Nish


                Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                My latest book : C++/CLI in Action / Amazon.com link

                S Offline
                S Offline
                Shog9 0
                wrote on last edited by
                #21

                :cool:

                You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...

                N 1 Reply Last reply
                0
                • S Shog9 0

                  :cool:

                  You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...

                  N Offline
                  N Offline
                  Nish Nishant
                  wrote on last edited by
                  #22

                  Thank you :-)

                  Regards, Nish


                  Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                  My latest book : C++/CLI in Action / Amazon.com link

                  1 Reply Last reply
                  0
                  • S Shog9 0

                    var list1 = ['A','B','C','D'];
                    var list2 = ['B','A','X','S','L','D'];

                    var result = list1.filter(function(i) list2.indexOf(i) >= 0)
                    .concat( list2.filter(function(i) list1.indexOf(i) < 0 ) );

                    Javascript 1.8 (tested on Firefox 3.0.1)

                    You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...

                    N Offline
                    N Offline
                    Nish Nishant
                    wrote on last edited by
                    #23

                    Ah you have a one-liner too I see - in fact the same algorithm as the one in mine.

                    Regards, Nish


                    Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                    My latest book : C++/CLI in Action / Amazon.com link

                    S 1 Reply Last reply
                    0
                    • N Nish Nishant

                      Ah you have a one-liner too I see - in fact the same algorithm as the one in mine.

                      Regards, Nish


                      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                      My latest book : C++/CLI in Action / Amazon.com link

                      S Offline
                      S Offline
                      Shog9 0
                      wrote on last edited by
                      #24

                      Yeah, same algorithm, but more verbose so i split it into two lines.

                      You must be careful in the forest Broken glass and rusty nails If you're to bring back something for us I have bullets for sale...

                      1 Reply Last reply
                      0
                      • R Rama Krishna Vavilala

                        Back on Popular demand You are given two lists. List 1 contains certain strings in a particular order. List 2 contains all the allowed values in List 1. List 1, however, can contain some elements not in List 2. The objective is to generate a List 3 which will have elements from List 1 in exactly the same order specified in List 1 followed by elements not in List 1 but present in List 2. Any elements not in List 2 should not be included. Example: List 1:

                        A,B,C,D

                        List 2:

                        B,A,X,S,L,D

                        Output:

                        A,B,D,X,S,L


                        Last modified: 10mins after originally posted --

                        Proud to be a CPHog user

                        J Offline
                        J Offline
                        Joe Woodbury
                        wrote on last edited by
                        #25

                        This is Friday, so my steps were: 1) Pick up phone 2) Call junior programmer Hank 3) Tell him this is due ASAP 4) Go home

                        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                        G 1 Reply Last reply
                        0
                        • R Rama Krishna Vavilala

                          Back on Popular demand You are given two lists. List 1 contains certain strings in a particular order. List 2 contains all the allowed values in List 1. List 1, however, can contain some elements not in List 2. The objective is to generate a List 3 which will have elements from List 1 in exactly the same order specified in List 1 followed by elements not in List 1 but present in List 2. Any elements not in List 2 should not be included. Example: List 1:

                          A,B,C,D

                          List 2:

                          B,A,X,S,L,D

                          Output:

                          A,B,D,X,S,L


                          Last modified: 10mins after originally posted --

                          Proud to be a CPHog user

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

                          I think my Set[^] class/struct was my first article. (It wasn't, but I wrote the class before the others.)

                          PIEBALD.Types.Set<char> a = new PIEBALD.Types.Set<char> ( 'A' , 'B' , 'C' , 'D' ) ;
                          PIEBALD.Types.Set<char> b = new PIEBALD.Types.Set<char> ( 'B' , 'A' , 'X' , 'S' , 'L' , 'D' ) ;

                          foreach ( char c in ( a & b ) + b ) ...

                          A HashSet didn't preserve the order, and isn't as expressive. X|

                          System.Collections.Generic.HashSet<char> c = new System.Collections.Generic.HashSet<char> ( a ) ;
                          System.Collections.Generic.HashSet<char> d = new System.Collections.Generic.HashSet<char> ( b ) ;

                          System.Collections.Generic.HashSet<char> f = c ;
                          f.IntersectWith ( d ) ;
                          f.UnionWith ( d ) ;

                          foreach ( char e in f )

                          I've been considering reworking Set to use a HashSet rather than a Dictionary (originally it used a Hashtable).

                          modified on Friday, August 1, 2008 9:08 PM

                          R 1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            I think my Set[^] class/struct was my first article. (It wasn't, but I wrote the class before the others.)

                            PIEBALD.Types.Set<char> a = new PIEBALD.Types.Set<char> ( 'A' , 'B' , 'C' , 'D' ) ;
                            PIEBALD.Types.Set<char> b = new PIEBALD.Types.Set<char> ( 'B' , 'A' , 'X' , 'S' , 'L' , 'D' ) ;

                            foreach ( char c in ( a & b ) + b ) ...

                            A HashSet didn't preserve the order, and isn't as expressive. X|

                            System.Collections.Generic.HashSet<char> c = new System.Collections.Generic.HashSet<char> ( a ) ;
                            System.Collections.Generic.HashSet<char> d = new System.Collections.Generic.HashSet<char> ( b ) ;

                            System.Collections.Generic.HashSet<char> f = c ;
                            f.IntersectWith ( d ) ;
                            f.UnionWith ( d ) ;

                            foreach ( char e in f )

                            I've been considering reworking Set to use a HashSet rather than a Dictionary (originally it used a Hashtable).

                            modified on Friday, August 1, 2008 9:08 PM

                            R Offline
                            R Offline
                            Rama Krishna Vavilala
                            wrote on last edited by
                            #27

                            PIEBALDconsult wrote:

                            ( a & b ) + b

                            It took me a while to understand that code:)

                            Proud to be a CPHog user

                            P 1 Reply Last reply
                            0
                            • R Rama Krishna Vavilala

                              PIEBALDconsult wrote:

                              ( a & b ) + b

                              It took me a while to understand that code:)

                              Proud to be a CPHog user

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

                              I had to access my article to figure out what operator I used for intersection. Then found that the order of operations is wrong. :( Ha! a & b | b works as required. I've been looking forward to a good FPQ for ages, I keep trying to think of my own.

                              1 Reply Last reply
                              0
                              • E Ennis Ray Lynch Jr

                                your kidding right?

                                Need a C# Consultant? I'm available.
                                Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

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

                                Ennis Ray Lynch, Jr. wrote:

                                your kidding right?

                                Sorry, I went to sleep :) No, I wasn't kidding. I meant what is n in relation to the problem. Does it refer to the 'variable' amount of 'squares'? Does it refer to the 'variable' amount of winning combinations? Does it refer to the 'width' of the board? Is it the number of sheep I count before I pass out? From your problem statement, all of the above are constant, (9, 8, 3, 42). You get what I am saying? Remember O(3000000000n) is still O(n).

                                xacc.ide - now with TabsToSpaces support
                                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                                E 1 Reply Last reply
                                0
                                • R Rama Krishna Vavilala

                                  Back on Popular demand You are given two lists. List 1 contains certain strings in a particular order. List 2 contains all the allowed values in List 1. List 1, however, can contain some elements not in List 2. The objective is to generate a List 3 which will have elements from List 1 in exactly the same order specified in List 1 followed by elements not in List 1 but present in List 2. Any elements not in List 2 should not be included. Example: List 1:

                                  A,B,C,D

                                  List 2:

                                  B,A,X,S,L,D

                                  Output:

                                  A,B,D,X,S,L


                                  Last modified: 10mins after originally posted --

                                  Proud to be a CPHog user

                                  S Offline
                                  S Offline
                                  Stuart Dootson
                                  wrote on last edited by
                                  #30

                                  In Haskell:

                                  import Data.List
                                  
                                  munge a b = (a `intersect` b) ++ (b \\ a)
                                  

                                  a `intersect` b retrieves the stable intersection of a with b. b \\ a takes all elements in a out of b. Job's a good'un! [edit]Should have read the Data.List documentation *before* answering rather than after! The code should be

                                  munge a b = a `intersect` b ++ filter (not.(`elem` a)) b
                                  

                                  \\ only deletes the first instances of elements of b that are also in a, i.e. ("ABA"\\"A" == "BA" which is not what's wanted here) [/edit]

                                  modified on Saturday, August 2, 2008 7:13 AM

                                  1 Reply Last reply
                                  0
                                  • R Rama Krishna Vavilala

                                    Back on Popular demand You are given two lists. List 1 contains certain strings in a particular order. List 2 contains all the allowed values in List 1. List 1, however, can contain some elements not in List 2. The objective is to generate a List 3 which will have elements from List 1 in exactly the same order specified in List 1 followed by elements not in List 1 but present in List 2. Any elements not in List 2 should not be included. Example: List 1:

                                    A,B,C,D

                                    List 2:

                                    B,A,X,S,L,D

                                    Output:

                                    A,B,D,X,S,L


                                    Last modified: 10mins after originally posted --

                                    Proud to be a CPHog user

                                    G Offline
                                    G Offline
                                    Gary R Wheeler
                                    wrote on last edited by
                                    #31

                                    CTypedPtrList<CPtrList,_TCHAR *> list1,list2,list3;
                                     
                                    list1.AddTail(_T("A"));
                                    list1.AddTail(_T("B"));
                                    list1.AddTail(_T("C"));
                                    list1.AddTail(_T("D"));
                                     
                                    list2.AddTail(_T("B"));
                                    list2.AddTail(_T("A"));
                                    list2.AddTail(_T("X"));
                                    list2.AddTail(_T("S"));
                                    list2.AddTail(_T("L"));
                                    list2.AddTail(_T("D"));
                                     
                                    POSITION p1 = list1.GetHeadPosition();
                                     
                                    while (p1 != NULL) {
                                     
                                    _TCHAR *s1 = list1.GetNext(p1);
                                     
                                    POSITION p2 = list2.GetHeadPosition();
                                     
                                    while (p2 != NULL) {
                                     
                                    POSITION r2 = p2;
                                     
                                    _TCHAR *s2 = list2.GetNext(p2);
                                     
                                    if (_tcsicmp(s1,s2) == 0) {
                                    list3.AddTail(s1);
                                    list2.RemoveAt(r2);
                                    }
                                     
                                    }
                                     
                                    }
                                     
                                    list3.AddTail(&list2);

                                    BTW: I did compile and run this; it works. While it does alter list2 in the process, a version that doesn't wouldn't be difficult.

                                    Software Zen: delete this;
                                    Fold With Us![^]

                                    1 Reply Last reply
                                    0
                                    • J Joe Woodbury

                                      This is Friday, so my steps were: 1) Pick up phone 2) Call junior programmer Hank 3) Tell him this is due ASAP 4) Go home

                                      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                                      G Offline
                                      G Offline
                                      Gary R Wheeler
                                      wrote on last edited by
                                      #32

                                      Gone over to the Dark Side*, have we hmm? * I'll be polite and not use the 'm'-word.

                                      Software Zen: delete this;
                                      Fold With Us![^]

                                      J 1 Reply Last reply
                                      0
                                      • L leppie

                                        Ennis Ray Lynch, Jr. wrote:

                                        your kidding right?

                                        Sorry, I went to sleep :) No, I wasn't kidding. I meant what is n in relation to the problem. Does it refer to the 'variable' amount of 'squares'? Does it refer to the 'variable' amount of winning combinations? Does it refer to the 'width' of the board? Is it the number of sheep I count before I pass out? From your problem statement, all of the above are constant, (9, 8, 3, 42). You get what I am saying? Remember O(3000000000n) is still O(n).

                                        xacc.ide - now with TabsToSpaces support
                                        IronScheme - 1.0 alpha 4a out now (29 May 2008)

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

                                        I think you may have a fundamental misunderstanding of Algorithm efficiency analysis and I am not going to explain it because, like I said it is a large topic. I don't mean to be rude about it I am just not in a pedantic mood at the moment and the books on the subject do a much better job than I would.

                                        Need a C# Consultant? I'm available.
                                        Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

                                        L 1 Reply Last reply
                                        0
                                        • E Ennis Ray Lynch Jr

                                          I think you may have a fundamental misunderstanding of Algorithm efficiency analysis and I am not going to explain it because, like I said it is a large topic. I don't mean to be rude about it I am just not in a pedantic mood at the moment and the books on the subject do a much better job than I would.

                                          Need a C# Consultant? I'm available.
                                          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

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

                                          Ennis Ray Lynch, Jr. wrote:

                                          I think you may have a fundamental misunderstanding of Algorithm efficiency analysis

                                          No, I dont, you are misunderstanding what I am asking you. The way I see it, given the constant nature (the board size does not change, neither does the total number of winning combinations) of your problem statement, and the small size of the problem, this problem would probably be solved in O(1), or O(log n) in the worst case where n would refer to the (constant) size of the board. This would be using a precomputed lookup table/tree for all game states (3^9). Now, if n was not constant, I would likely not be able to do so. But even in that case, the problem should not take more than O(n). Maybe I am reading/misunderstanding your problem statement. :~

                                          xacc.ide - now with TabsToSpaces support
                                          IronScheme - 1.0 alpha 4a out now (29 May 2008)

                                          E 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