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

    J Offline
    J Offline
    Jeslan
    wrote on last edited by
    #5

    If list 1 and list 2 are both size of an order 'n': 1. Create a sorted List 2 (n*log n) 2. Binary search the sorted list 2 for each element in list 1 and append the findings to list 3 (n*log n) 3. Create a sorted List 1 (n*log n) 4. Binary search the sorted list 1 for each element in list 2 and append the missing ones to list 3 (n*log n) 5. Output list 3 O(n*log n) time with O(n) extra memory allocated

    1 Reply Last reply
    0
    • P Paul Conrad

      Rama, Shouldn't the C in List1 be included in List3? PJC

      "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

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

      No

      Proud to be a CPHog user

      1 Reply Last reply
      0
      • C Chris Losinger

        Rama Krishna Vavilala wrote:

        List 2 contains all the allowed values in List 1. List 1, however, can contain some elements not in List 2.

        can you rephrase this? if list 2 contains the values which are allowed in list 1, how can list 1 contain values which aren't in list 2?

        image processing toolkits | batch image processing

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

        Chris Losinger wrote:

        how can list 1 contain values which aren't in list 2?

        Those should not have been there but are there so they need to be taken out. So at one point of time the value was allowed but then at a further point the value is not allowed. The real life case where the situation occurred: List 1 was a subset of column names in a table. List 2 was all the column names. At one point someone may add or remove columns in a table (List 2). In that case List 1 *may* have some invalid values.

        Proud to be a CPHog user

        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

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

          Judah Himango wrote:

          Is it cheating to use LINQ?

          No LINQ is definitely one of the reasons for these quizzes.:)

          Proud to be a CPHog user

          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

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

            Also all these questions are based on real life situations I faced and where I(or some other team member) had to use LINQ to objects.

            Proud to be a CPHog user

            J 1 Reply Last reply
            0
            • R Rama Krishna Vavilala

              Also all these questions are based on real life situations I faced and where I(or some other team member) had to use LINQ to objects.

              Proud to be a CPHog user

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #10

              Hey man, you're preaching to the choir. We've been using our own LINQ-like library for .NET 2 (uglier b/c no extension methods, less type inference), and even there it's been invaluable to us where we have to gather parts of a collection, or parts of objects in a collection.

              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

              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

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

                Simple, how about a hard one? Given a tic-tac-toe board where the board can be represented by a one-dimensional array of chars with 'X' being x, 'Y' being y, and all others being empty such that the top left most square is the 0 index, followed by the middle square on the top row as the 1 index ....

                0 | 1 | 2
                _____________
                3 | 4 | 5
                _____________
                6 | 7 | 8

                Write an algorithm to display the number of winning combination available for each X and O based on the game state when passed an array in under n^2.

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

                R L 2 Replies Last reply
                0
                • E Ennis Ray Lynch Jr

                  Simple, how about a hard one? Given a tic-tac-toe board where the board can be represented by a one-dimensional array of chars with 'X' being x, 'Y' being y, and all others being empty such that the top left most square is the 0 index, followed by the middle square on the top row as the 1 index ....

                  0 | 1 | 2
                  _____________
                  3 | 4 | 5
                  _____________
                  6 | 7 | 8

                  Write an algorithm to display the number of winning combination available for each X and O based on the game state when passed an array in under n^2.

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

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

                  Ennis Ray Lynch, Jr. wrote:

                  Simple,

                  Friday is meant for simple quizzes:)

                  Proud to be a CPHog user

                  1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    Simple, how about a hard one? Given a tic-tac-toe board where the board can be represented by a one-dimensional array of chars with 'X' being x, 'Y' being y, and all others being empty such that the top left most square is the 0 index, followed by the middle square on the top row as the 1 index ....

                    0 | 1 | 2
                    _____________
                    3 | 4 | 5
                    _____________
                    6 | 7 | 8

                    Write an algorithm to display the number of winning combination available for each X and O based on the game state when passed an array in under n^2.

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

                    Ha! I think I did this in first year uni :) Wow, I did some scary (but still cool) code back then, in C!

                    Ennis Ray Lynch, Jr. wrote:

                    under n^2.

                    What is n suppose to be?

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

                    E 1 Reply Last reply
                    0
                    • L leppie

                      Ha! I think I did this in first year uni :) Wow, I did some scary (but still cool) code back then, in C!

                      Ennis Ray Lynch, Jr. wrote:

                      under n^2.

                      What is n suppose to be?

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

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