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. Coding Challenge

Coding Challenge

Scheduled Pinned Locked Moved The Lounge
c++architecturehelp
165 Posts 47 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.
  • C CPallini

    Hey Chris, may I post my own programming issue labeling it with 'Coding Challenge' or 'Fun For Everyone'? Just Kidding! Sorry for my poor understanding of English, with

    Chris Maunder wrote:

    from either end

    Do you mean the output string should be:

    " monkey dog horse "

    ?

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

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

    I am just glad he wants a Monkey Dog Horse and not one of these[^].

    ------------------------------------ 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[^] Trolls[^]

    1 Reply Last reply
    0
    • L Lost User

      You can think it means whatever you want, but in correct English usage either can mean both 'one of two' or 'each of two.

      Every man can tell how many goats or sheep he possesses, but not how many friends.

      H Offline
      H Offline
      hairy_hats
      wrote on last edited by
      #25

      Schrodinger's cat was either alive or dead, but not both. ;)

      L 1 Reply Last reply
      0
      • C Chris Maunder

        Back in the Days of Yore we had a couple of small coding challenges such as the Lean and Mean comp. I was thinking that there are a ton of small, well defined problems that can be tackled a zillion ways in a zillion languages and that it would be cool to see what you guys can come up with. I'd like to start the ball rolling with the following simple task: Problem: Given a string of text, trim from each end of the text each all occurrences of a given set of strings Sample input: Input string: "dog cat monkey dog horse dog" Strings that need to be trimmed from each end: { "dog", "cat" } Final output should be: " monkey dog horse" Final output should be " cat monkey dog horse " [Edit: My final sample output was incorrect, so to be fair I'll accept either answer] It's up to you whether you worry about case sensitivity. Let's see who can provide the smallest, neatest most elegant, most unique and/or fastest code. For those who feel like jumping on the "No Programming questions" bandwagon, please re-read the lounge guidelines. The point of this is to have fun, not to solve each other's programming issues.

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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #26

        Hmmmm, 2 KISS algorithms A two pass algorithm might be: 1.) Tokenize the string using white space characters. 2.) Moving from left to right, push the string into a deque if the string is not in the excluded list. If we encounter a string that is included, all proceeding tags are pushed into the deque. 3.) Then do the same from right to left. A single pass algorithm might be: 1.) Tokenize the string using white space characters. 2.) Push all tags into a pair of string:index 3.) For each pair of string and index, if the string is excluded... if the index value of all other string:index pairs before me are also excluded... My index is the beginning of the string. 4.) For each pair of string and index, if the string is excluded... if the index value of all other string:index pairs after me are also excluded... My index is the end of the string. :) I'm too lazy to write the code. Btw, I think the single pass algorithm could be implemented in a single lambda line of code. Best Wishes, -David Delaune

        C H V C P 5 Replies Last reply
        0
        • H hairy_hats

          Schrodinger's cat was either alive or dead, but not both. ;)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #27

          It thought it was both alive and dead until you had a look in the box at which point it became alive or dead. A bit late but still; Erwin Schrödinger has sent us a Christmas present. The kids are going to be delighted or distraught on Christmas Day.

          Every man can tell how many goats or sheep he possesses, but not how many friends.

          H I 2 Replies Last reply
          0
          • C Chris Maunder

            Back in the Days of Yore we had a couple of small coding challenges such as the Lean and Mean comp. I was thinking that there are a ton of small, well defined problems that can be tackled a zillion ways in a zillion languages and that it would be cool to see what you guys can come up with. I'd like to start the ball rolling with the following simple task: Problem: Given a string of text, trim from each end of the text each all occurrences of a given set of strings Sample input: Input string: "dog cat monkey dog horse dog" Strings that need to be trimmed from each end: { "dog", "cat" } Final output should be: " monkey dog horse" Final output should be " cat monkey dog horse " [Edit: My final sample output was incorrect, so to be fair I'll accept either answer] It's up to you whether you worry about case sensitivity. Let's see who can provide the smallest, neatest most elegant, most unique and/or fastest code. For those who feel like jumping on the "No Programming questions" bandwagon, please re-read the lounge guidelines. The point of this is to have fun, not to solve each other's programming issues.

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

            V Offline
            V Offline
            vonb
            wrote on last edited by
            #28

            // Well, this is my piece (C#): static void main(string[] args) { string Input = "doc cat monkey dog horse dog"; List listDeleteStart = new List(); List ListDeleteEnd = new List (); //Output = " monkey dog horse " string[] toTrim = new string[] {"dog", "cat"}; string[] InputStrip = Input.Split(new char[] {' '}); int iCounter = 0; //Going forwards foreach(string strSingle in InputString) { if(toTrim.Contains(strSingle)) ListDeleteStart.Add(iCounter); else break; iCounter++; } //Going backwards InputString = InputString.Reverse().ToArray(); iCounter = InputString.Length-1; foreach(string strSingleRev in InputString) { if(toTrim.Contains(strSingleRev)) ListDeleteStart.Add(iCounter); else break; iCounter--; } //Putting it back again for analysis InputString = InputString.Reverse().ToArray(); string Output = ""; iCounter = 0; foreach (string strResult in InputString) { if(!listDeleteStart.Contains(iCounter) && !listDeletedEnd.Contains(iCounter)) Output+= " " + strResult + " "; iCounter++; } Console.WriteLine(Output); Console.ReadKey(); }

            N 1 Reply Last reply
            0
            • L Lost User

              Hmmmm, 2 KISS algorithms A two pass algorithm might be: 1.) Tokenize the string using white space characters. 2.) Moving from left to right, push the string into a deque if the string is not in the excluded list. If we encounter a string that is included, all proceeding tags are pushed into the deque. 3.) Then do the same from right to left. A single pass algorithm might be: 1.) Tokenize the string using white space characters. 2.) Push all tags into a pair of string:index 3.) For each pair of string and index, if the string is excluded... if the index value of all other string:index pairs before me are also excluded... My index is the beginning of the string. 4.) For each pair of string and index, if the string is excluded... if the index value of all other string:index pairs after me are also excluded... My index is the end of the string. :) I'm too lazy to write the code. Btw, I think the single pass algorithm could be implemented in a single lambda line of code. Best Wishes, -David Delaune

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #29

              Randor wrote:

              Tokenize the string using white space characters.

              is that a valid assumption? dogcathorsefoodcat

              image processing toolkits | batch image processing

              L 1 Reply Last reply
              0
              • L Lost User

                It thought it was both alive and dead until you had a look in the box at which point it became alive or dead. A bit late but still; Erwin Schrödinger has sent us a Christmas present. The kids are going to be delighted or distraught on Christmas Day.

                Every man can tell how many goats or sheep he possesses, but not how many friends.

                H Offline
                H Offline
                hairy_hats
                wrote on last edited by
                #30

                Not "either alive and dead"? ;) In any case, the cat is itself an observer, so the waveform is always collapsed to a known state or "alive" or "dead", so it's a silly thought experiment (IMHO).

                P L 2 Replies Last reply
                0
                • L Lost User

                  Hmmmm, 2 KISS algorithms A two pass algorithm might be: 1.) Tokenize the string using white space characters. 2.) Moving from left to right, push the string into a deque if the string is not in the excluded list. If we encounter a string that is included, all proceeding tags are pushed into the deque. 3.) Then do the same from right to left. A single pass algorithm might be: 1.) Tokenize the string using white space characters. 2.) Push all tags into a pair of string:index 3.) For each pair of string and index, if the string is excluded... if the index value of all other string:index pairs before me are also excluded... My index is the beginning of the string. 4.) For each pair of string and index, if the string is excluded... if the index value of all other string:index pairs after me are also excluded... My index is the end of the string. :) I'm too lazy to write the code. Btw, I think the single pass algorithm could be implemented in a single lambda line of code. Best Wishes, -David Delaune

                  V Offline
                  V Offline
                  vonb
                  wrote on last edited by
                  #31

                  Just did it, watch below.

                  1 Reply Last reply
                  0
                  • C Chris Maunder

                    Back in the Days of Yore we had a couple of small coding challenges such as the Lean and Mean comp. I was thinking that there are a ton of small, well defined problems that can be tackled a zillion ways in a zillion languages and that it would be cool to see what you guys can come up with. I'd like to start the ball rolling with the following simple task: Problem: Given a string of text, trim from each end of the text each all occurrences of a given set of strings Sample input: Input string: "dog cat monkey dog horse dog" Strings that need to be trimmed from each end: { "dog", "cat" } Final output should be: " monkey dog horse" Final output should be " cat monkey dog horse " [Edit: My final sample output was incorrect, so to be fair I'll accept either answer] It's up to you whether you worry about case sensitivity. Let's see who can provide the smallest, neatest most elegant, most unique and/or fastest code. For those who feel like jumping on the "No Programming questions" bandwagon, please re-read the lounge guidelines. The point of this is to have fun, not to solve each other's programming issues.

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

                    R Offline
                    R Offline
                    Rajesh Anuhya
                    wrote on last edited by
                    #32

                    Quote:

                    Quote:text each all occurrences of a given set of strings

                    I think final output should be "Final output should be: " monkey horse ".

                    My Tip/Trick[^]

                    S H 2 Replies Last reply
                    0
                    • L Lost User

                      Hmmmm, 2 KISS algorithms A two pass algorithm might be: 1.) Tokenize the string using white space characters. 2.) Moving from left to right, push the string into a deque if the string is not in the excluded list. If we encounter a string that is included, all proceeding tags are pushed into the deque. 3.) Then do the same from right to left. A single pass algorithm might be: 1.) Tokenize the string using white space characters. 2.) Push all tags into a pair of string:index 3.) For each pair of string and index, if the string is excluded... if the index value of all other string:index pairs before me are also excluded... My index is the beginning of the string. 4.) For each pair of string and index, if the string is excluded... if the index value of all other string:index pairs after me are also excluded... My index is the end of the string. :) I'm too lazy to write the code. Btw, I think the single pass algorithm could be implemented in a single lambda line of code. Best Wishes, -David Delaune

                      H Offline
                      H Offline
                      hairy_hats
                      wrote on last edited by
                      #33

                      Randor wrote:

                      I think the single pass algorithm could be implemented in a single lambda line of code.

                      The Not-So-KISS solution.

                      L 1 Reply Last reply
                      0
                      • R Rajesh Anuhya

                        Quote:

                        Quote:text each all occurrences of a given set of strings

                        I think final output should be "Final output should be: " monkey horse ".

                        My Tip/Trick[^]

                        H Offline
                        H Offline
                        hairy_hats
                        wrote on last edited by
                        #34

                        No, Chris said only the ends of the string were to be trimmed, not the centre.

                        1 Reply Last reply
                        0
                        • C Chris Maunder

                          Back in the Days of Yore we had a couple of small coding challenges such as the Lean and Mean comp. I was thinking that there are a ton of small, well defined problems that can be tackled a zillion ways in a zillion languages and that it would be cool to see what you guys can come up with. I'd like to start the ball rolling with the following simple task: Problem: Given a string of text, trim from each end of the text each all occurrences of a given set of strings Sample input: Input string: "dog cat monkey dog horse dog" Strings that need to be trimmed from each end: { "dog", "cat" } Final output should be: " monkey dog horse" Final output should be " cat monkey dog horse " [Edit: My final sample output was incorrect, so to be fair I'll accept either answer] It's up to you whether you worry about case sensitivity. Let's see who can provide the smallest, neatest most elegant, most unique and/or fastest code. For those who feel like jumping on the "No Programming questions" bandwagon, please re-read the lounge guidelines. The point of this is to have fun, not to solve each other's programming issues.

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

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

                          Just out of fun as this is fun I created this in VB.NET

                          Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                          Dim Input As String = "dog cat monkey dog horse dog"
                          Dim ToRemove As String() = New String() {"dog", "cat"}
                          Dim Start As Integer = 0
                          Dim EndPointer As Integer = 0

                              For Each Value As String In ToRemove
                                  If Start < Input.IndexOf(Value) Then Start = Input.IndexOf(Value) + Value.Length
                                  If EndPointer < Input.LastIndexOf(Value) Then EndPointer = Input.LastIndexOf(Value)
                              Next
                          
                              MessageBox.Show(Input.Substring(Start, (EndPointer - Start)))
                          End Sub
                          

                          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

                          N C 2 Replies Last reply
                          0
                          • C Chris Maunder

                            Back in the Days of Yore we had a couple of small coding challenges such as the Lean and Mean comp. I was thinking that there are a ton of small, well defined problems that can be tackled a zillion ways in a zillion languages and that it would be cool to see what you guys can come up with. I'd like to start the ball rolling with the following simple task: Problem: Given a string of text, trim from each end of the text each all occurrences of a given set of strings Sample input: Input string: "dog cat monkey dog horse dog" Strings that need to be trimmed from each end: { "dog", "cat" } Final output should be: " monkey dog horse" Final output should be " cat monkey dog horse " [Edit: My final sample output was incorrect, so to be fair I'll accept either answer] It's up to you whether you worry about case sensitivity. Let's see who can provide the smallest, neatest most elegant, most unique and/or fastest code. For those who feel like jumping on the "No Programming questions" bandwagon, please re-read the lounge guidelines. The point of this is to have fun, not to solve each other's programming issues.

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

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

                            I have discovered a truly marvellous solution of this, which the margin of this website is too narrow to contain.

                            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

                            C L L 3 Replies Last reply
                            0
                            • R Rajesh Anuhya

                              Quote:

                              Quote:text each all occurrences of a given set of strings

                              I think final output should be "Final output should be: " monkey horse ".

                              My Tip/Trick[^]

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

                              No as you are removing all instances of dog and cat from the string and not from either end of the string

                              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

                                Back in the Days of Yore we had a couple of small coding challenges such as the Lean and Mean comp. I was thinking that there are a ton of small, well defined problems that can be tackled a zillion ways in a zillion languages and that it would be cool to see what you guys can come up with. I'd like to start the ball rolling with the following simple task: Problem: Given a string of text, trim from each end of the text each all occurrences of a given set of strings Sample input: Input string: "dog cat monkey dog horse dog" Strings that need to be trimmed from each end: { "dog", "cat" } Final output should be: " monkey dog horse" Final output should be " cat monkey dog horse " [Edit: My final sample output was incorrect, so to be fair I'll accept either answer] It's up to you whether you worry about case sensitivity. Let's see who can provide the smallest, neatest most elegant, most unique and/or fastest code. For those who feel like jumping on the "No Programming questions" bandwagon, please re-read the lounge guidelines. The point of this is to have fun, not to solve each other's programming issues.

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

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

                                Perl, perhaps. One line, maybe two.

                                Just along for the ride. "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)

                                1 Reply Last reply
                                0
                                • D Dalek Dave

                                  How can anything be described as "Most Unique"? Unique is an absolute, it either is or isn't. Perhaps "Most Elegant" would be a more apposite epithet?

                                  ------------------------------------ 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[^] Trolls[^]

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

                                  You going to code or are you going to nitpick? ;)

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

                                  1 Reply Last reply
                                  0
                                  • C Chris Maunder

                                    Back in the Days of Yore we had a couple of small coding challenges such as the Lean and Mean comp. I was thinking that there are a ton of small, well defined problems that can be tackled a zillion ways in a zillion languages and that it would be cool to see what you guys can come up with. I'd like to start the ball rolling with the following simple task: Problem: Given a string of text, trim from each end of the text each all occurrences of a given set of strings Sample input: Input string: "dog cat monkey dog horse dog" Strings that need to be trimmed from each end: { "dog", "cat" } Final output should be: " monkey dog horse" Final output should be " cat monkey dog horse " [Edit: My final sample output was incorrect, so to be fair I'll accept either answer] It's up to you whether you worry about case sensitivity. Let's see who can provide the smallest, neatest most elegant, most unique and/or fastest code. For those who feel like jumping on the "No Programming questions" bandwagon, please re-read the lounge guidelines. The point of this is to have fun, not to solve each other's programming issues.

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

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

                                    No to be pedantic about things, bit if you're tokenizing based on "dog" and "cat", your final answer should be "  monkey dog horse " (begins with 2 spaces) or "monkey dog horse " (begins with no space). Otherwise, the requirement on what to do with spaces is incomplete. :)

                                    Web - BM - RSS - Math - LinkedIn

                                    C 1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      Back in the Days of Yore we had a couple of small coding challenges such as the Lean and Mean comp. I was thinking that there are a ton of small, well defined problems that can be tackled a zillion ways in a zillion languages and that it would be cool to see what you guys can come up with. I'd like to start the ball rolling with the following simple task: Problem: Given a string of text, trim from each end of the text each all occurrences of a given set of strings Sample input: Input string: "dog cat monkey dog horse dog" Strings that need to be trimmed from each end: { "dog", "cat" } Final output should be: " monkey dog horse" Final output should be " cat monkey dog horse " [Edit: My final sample output was incorrect, so to be fair I'll accept either answer] It's up to you whether you worry about case sensitivity. Let's see who can provide the smallest, neatest most elegant, most unique and/or fastest code. For those who feel like jumping on the "No Programming questions" bandwagon, please re-read the lounge guidelines. The point of this is to have fun, not to solve each other's programming issues.

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

                                      J Offline
                                      J Offline
                                      JacquesDP
                                      wrote on last edited by
                                      #41

                                      static void Main(string[] args)
                                      {
                                      string testString = "dog cat monkey dog horse dog";
                                      List splitString = new List(testString.Trim().Split(new char[]{' '}));
                                      for (int index = 0; index < splitString.Count; index++)
                                      {
                                      if (splitString[0].Trim().Equals("dog") || splitString[0].Trim().Equals("cat"))
                                      splitString.RemoveAt(0);
                                      else if (splitString[splitString.Count - 1].Trim().Equals("dog") || splitString[splitString.Count - 1].Trim().Equals("cat"))
                                      splitString.RemoveAt(splitString.Count - 1);
                                      else
                                      break;
                                      index = 0;
                                      }
                                      string final = "";
                                      foreach (var entry in splitString)
                                      final += entry + " ";
                                      Console.WriteLine(final.Trim());
                                      Console.ReadKey();
                                      }

                                      No matter how long he who laughs last laughs, he who laughs first has a head start!

                                      1 Reply Last reply
                                      0
                                      • H hairy_hats

                                        How should whitespace at the ends of the input string be treated - does " cat dog " match or only "cat dog"?

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

                                        The challenge is to remove the strings that are provided. Nothing is said about removing (or, indeed, caring about, whitespace)

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

                                        H 1 Reply Last reply
                                        0
                                        • V vonb

                                          // Well, this is my piece (C#): static void main(string[] args) { string Input = "doc cat monkey dog horse dog"; List listDeleteStart = new List(); List ListDeleteEnd = new List (); //Output = " monkey dog horse " string[] toTrim = new string[] {"dog", "cat"}; string[] InputStrip = Input.Split(new char[] {' '}); int iCounter = 0; //Going forwards foreach(string strSingle in InputString) { if(toTrim.Contains(strSingle)) ListDeleteStart.Add(iCounter); else break; iCounter++; } //Going backwards InputString = InputString.Reverse().ToArray(); iCounter = InputString.Length-1; foreach(string strSingleRev in InputString) { if(toTrim.Contains(strSingleRev)) ListDeleteStart.Add(iCounter); else break; iCounter--; } //Putting it back again for analysis InputString = InputString.Reverse().ToArray(); string Output = ""; iCounter = 0; foreach (string strResult in InputString) { if(!listDeleteStart.Contains(iCounter) && !listDeletedEnd.Contains(iCounter)) Output+= " " + strResult + " "; iCounter++; } Console.WriteLine(Output); Console.ReadKey(); }

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

                                          Failed: 1. No formatting 2. Language Dependent 3. Too verbose

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

                                          L 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