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 [modified]

Friday Programming Quiz [modified]

Scheduled Pinned Locked Moved The Lounge
debugging
39 Posts 23 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.
  • J Jon Sagara

    Plain English Function Called "Remove Duplicates" with Argument Consisting of Comma Separated Values in a Character String Remove Duplicate Values From The Plain English Function Argument Consisting of Comma Separated Values in a Character String Return The Plain English Function Argument Consisting of Comma Separated Values in a Character String, But With Duplicate Values Removed End Of Plain English Function Called "Remove Duplicates" with Argument Consisting of Comma Separated Values in a Character String Excuse me while I go hurl X|

    Jon Sagara When I grow up, I'm changing my name to Joe Kickass! My Site | My Blog | My Articles

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #3

    Ah - now I know what PE is.

    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

    1 Reply Last reply
    0
    • R Rama Krishna Vavilala

      In a language of your choice (no PE), implement the following:

      string RemoveDuplicates(string csvString) {

      }

      The function should remove all duplicate values form a string containing comma separated values. RemoveDuplicates("a,b,c,a") => "a,b,c" RemoveDuplicates("a,b,c,a,c,b,c") => "a,b,c" RemoveDuplicates("a,b,c") => "a,b,c" RemoveDuplicates("cat,dog,dog") => "cat,dog" The ideal implementation should have just 1 line of code.


      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

      T Offline
      T Offline
      ToddHileHoffer
      wrote on last edited by
      #4

      In C# string RemoveDuplicates(string csvString) { string[] x = csvString.Split(char.Parse(",")); System.Collections.Specialized.StringCollection c = new System.Collections.Specialized.StringCollection(); foreach (string y in x) { if (!c.Contains(y)) c.Add(y); } string result = ""; foreach (string z in c) { result += z + ","; } return result.Substring(0, result.Length - 1); }


      how vital enterprise application are for proactive organizations leveraging collective synergy to think outside the box and formulate their key objectives into a win-win game plan with a quality-driven approach that focuses on empowering key players to drive-up their core competencies and increase expectations with an all-around initiative to drive up the bottom-line. But of course, that's all a "high level" overview of things --thedailywtf 3/21/06

      D P 2 Replies Last reply
      0
      • R Rama Krishna Vavilala

        In a language of your choice (no PE), implement the following:

        string RemoveDuplicates(string csvString) {

        }

        The function should remove all duplicate values form a string containing comma separated values. RemoveDuplicates("a,b,c,a") => "a,b,c" RemoveDuplicates("a,b,c,a,c,b,c") => "a,b,c" RemoveDuplicates("a,b,c") => "a,b,c" RemoveDuplicates("cat,dog,dog") => "cat,dog" The ideal implementation should have just 1 line of code.


        Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

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

        /*
        Needs VC++ 8 to compile
        */
        string DoFunc(string s)
        {
        set<char> st;
        for each(char c in s)
        {
        st.insert(c);
        }
        st.erase(st.find(','));
        char *arr = new char[2 * st.size()];
        int index = 0;
        for each(char c in st)
        {
        arr[index++] = c;
        arr[index++] = ',';
        }
        arr[--index] = 0;
        s = arr;
        delete[] arr;
        return s;
        }

        Regards, Nish


        Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
        Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog

        R 1 Reply Last reply
        0
        • J Jon Sagara

          Plain English Function Called "Remove Duplicates" with Argument Consisting of Comma Separated Values in a Character String Remove Duplicate Values From The Plain English Function Argument Consisting of Comma Separated Values in a Character String Return The Plain English Function Argument Consisting of Comma Separated Values in a Character String, But With Duplicate Values Removed End Of Plain English Function Called "Remove Duplicates" with Argument Consisting of Comma Separated Values in a Character String Excuse me while I go hurl X|

          Jon Sagara When I grow up, I'm changing my name to Joe Kickass! My Site | My Blog | My Articles

          G Offline
          G Offline
          Gary Wheeler
          wrote on last edited by
          #6

          Looks like COBOL, minus the dashes. Minus. Dashes. :rolleyes:


          Software Zen: delete this;

          1 Reply Last reply
          0
          • N Nish Nishant

            /*
            Needs VC++ 8 to compile
            */
            string DoFunc(string s)
            {
            set<char> st;
            for each(char c in s)
            {
            st.insert(c);
            }
            st.erase(st.find(','));
            char *arr = new char[2 * st.size()];
            int index = 0;
            for each(char c in st)
            {
            arr[index++] = c;
            arr[index++] = ',';
            }
            arr[--index] = 0;
            s = arr;
            delete[] arr;
            return s;
            }

            Regards, Nish


            Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
            Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog

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

            Well, I should have stated that the values are strings not just single characters.


            Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

            E 1 Reply Last reply
            0
            • R Rama Krishna Vavilala

              In a language of your choice (no PE), implement the following:

              string RemoveDuplicates(string csvString) {

              }

              The function should remove all duplicate values form a string containing comma separated values. RemoveDuplicates("a,b,c,a") => "a,b,c" RemoveDuplicates("a,b,c,a,c,b,c") => "a,b,c" RemoveDuplicates("a,b,c") => "a,b,c" RemoveDuplicates("cat,dog,dog") => "cat,dog" The ideal implementation should have just 1 line of code.


              Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

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

              What's a line of code?

              R 1 Reply Last reply
              0
              • R Rama Krishna Vavilala

                In a language of your choice (no PE), implement the following:

                string RemoveDuplicates(string csvString) {

                }

                The function should remove all duplicate values form a string containing comma separated values. RemoveDuplicates("a,b,c,a") => "a,b,c" RemoveDuplicates("a,b,c,a,c,b,c") => "a,b,c" RemoveDuplicates("a,b,c") => "a,b,c" RemoveDuplicates("cat,dog,dog") => "cat,dog" The ideal implementation should have just 1 line of code.


                Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

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

                template < typename _Cont > void split(const std::string& str, _Cont& _container, const std::string& delim=",")
                {
                std::string::size_type lpos = 0;
                std::string::size_type pos = str.find_first_of(delim, lpos);
                while(lpos != std::string::npos)
                {
                _container.insert(_container.end(), str.substr(lpos,pos - lpos));

                    lpos = ( pos == std::string::npos ) ?  std::string::npos : pos + 1;
                    pos = str.find\_first\_of(delim, lpos);
                }
                

                }

                std::string fn(std::string in)
                {
                std::string out;
                std::set foo;
                split(in, foo);

                for (std::set::iterator it=foo.begin();it!=foo.end();it++)
                {
                	if ((\*it).size() > 0)
                	{
                		out+=(\*it);
                		if (std::distance(it, foo.end()) > 1) out+=",";
                	}
                }
                return out;
                

                }

                and you can count this as my code from CP entry for the day. why is IE (or CP?) putting that sentence inside the PRE ? it's not. it looked fine in FF2.0. -- modified at 19:14 Friday 3rd November, 2006

                image processing | blogging

                J 1 Reply Last reply
                0
                • R Rama Krishna Vavilala

                  In a language of your choice (no PE), implement the following:

                  string RemoveDuplicates(string csvString) {

                  }

                  The function should remove all duplicate values form a string containing comma separated values. RemoveDuplicates("a,b,c,a") => "a,b,c" RemoveDuplicates("a,b,c,a,c,b,c") => "a,b,c" RemoveDuplicates("a,b,c") => "a,b,c" RemoveDuplicates("cat,dog,dog") => "cat,dog" The ideal implementation should have just 1 line of code.


                  Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

                  D Offline
                  D Offline
                  Daniel Grunwald
                  wrote on last edited by
                  #10

                  This is what LINQ is for: return string.Join(",", csvString.Split(',').Distinct()); Edit: Note that it is also the most efficient solution - it's O(N) because Distinct() internally uses a hash table. The C++ set<> solutions are O(N log N), though probably faster in the real world. And everything running Contains() repeatedly will be O(N²). Not that anyone would store large amounts of data in CSV strings.... Second modification: Sadly, it won't work like that. Distinct() returns IEnumerable, but for some strange reason, Join only works with arrays. So if we don't get a new Join() overload in .NET 3.5, add a .ToArray() extension method call behind the Distinct().

                  Last modified: 24mins after originally posted --

                  M D M 3 Replies Last reply
                  0
                  • T ToddHileHoffer

                    In C# string RemoveDuplicates(string csvString) { string[] x = csvString.Split(char.Parse(",")); System.Collections.Specialized.StringCollection c = new System.Collections.Specialized.StringCollection(); foreach (string y in x) { if (!c.Contains(y)) c.Add(y); } string result = ""; foreach (string z in c) { result += z + ","; } return result.Substring(0, result.Length - 1); }


                    how vital enterprise application are for proactive organizations leveraging collective synergy to think outside the box and formulate their key objectives into a win-win game plan with a quality-driven approach that focuses on empowering key players to drive-up their core competencies and increase expectations with an all-around initiative to drive up the bottom-line. But of course, that's all a "high level" overview of things --thedailywtf 3/21/06

                    D Offline
                    D Offline
                    Daniel Grunwald
                    wrote on last edited by
                    #11

                    char.Parse?? Why that? Haven't you heard of character literals: string.Split(',') ? And concatenating using += is the easiest way to screw up your applications performance. For a list with just a few thousand items, you'll be copying multiple GB of RAM. Remember that every operation on a string creates a complete copy of that string, so the StringBuilder is much better here.

                    1 Reply Last reply
                    0
                    • J Jon Sagara

                      Plain English Function Called "Remove Duplicates" with Argument Consisting of Comma Separated Values in a Character String Remove Duplicate Values From The Plain English Function Argument Consisting of Comma Separated Values in a Character String Return The Plain English Function Argument Consisting of Comma Separated Values in a Character String, But With Duplicate Values Removed End Of Plain English Function Called "Remove Duplicates" with Argument Consisting of Comma Separated Values in a Character String Excuse me while I go hurl X|

                      Jon Sagara When I grow up, I'm changing my name to Joe Kickass! My Site | My Blog | My Articles

                      A Offline
                      A Offline
                      Andy Brummer
                      wrote on last edited by
                      #12

                      :-D Please tell me you just made that up. That isn't an actual example of PE programming, it can't be that would just be absurd.

                      Using the GridView is like trying to explain to someone else how to move a third person's hands in order to tie your shoelaces for you. -Chris Maunder

                      J A 2 Replies Last reply
                      0
                      • D Daniel Grunwald

                        This is what LINQ is for: return string.Join(",", csvString.Split(',').Distinct()); Edit: Note that it is also the most efficient solution - it's O(N) because Distinct() internally uses a hash table. The C++ set<> solutions are O(N log N), though probably faster in the real world. And everything running Contains() repeatedly will be O(N²). Not that anyone would store large amounts of data in CSV strings.... Second modification: Sadly, it won't work like that. Distinct() returns IEnumerable, but for some strange reason, Join only works with arrays. So if we don't get a new Join() overload in .NET 3.5, add a .ToArray() extension method call behind the Distinct().

                        Last modified: 24mins after originally posted --

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #13

                        Daniel Grunwald wrote:

                        This is what LINQ is for:

                        Very very cool! Marc

                        Thyme In The Country

                        People are just notoriously impossible. --DavidCrow
                        There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
                        People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith

                        1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          What's a line of code?

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

                          Depends on the language (probably it is better to call 1 statement instead of 1 line): Something like this[^]


                          Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

                          P 1 Reply Last reply
                          0
                          • A Andy Brummer

                            :-D Please tell me you just made that up. That isn't an actual example of PE programming, it can't be that would just be absurd.

                            Using the GridView is like trying to explain to someone else how to move a third person's hands in order to tie your shoelaces for you. -Chris Maunder

                            J Offline
                            J Offline
                            Jon Sagara
                            wrote on last edited by
                            #15

                            Andy Brummer wrote:

                            Please tell me you just made that up.

                            Totally. ;)

                            Jon Sagara When I grow up, I'm changing my name to Joe Kickass! My Site | My Blog | My Articles

                            1 Reply Last reply
                            0
                            • R Rama Krishna Vavilala

                              In a language of your choice (no PE), implement the following:

                              string RemoveDuplicates(string csvString) {

                              }

                              The function should remove all duplicate values form a string containing comma separated values. RemoveDuplicates("a,b,c,a") => "a,b,c" RemoveDuplicates("a,b,c,a,c,b,c") => "a,b,c" RemoveDuplicates("a,b,c") => "a,b,c" RemoveDuplicates("cat,dog,dog") => "cat,dog" The ideal implementation should have just 1 line of code.


                              Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

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

                              This uses a string parser class I wrote (which is available in both MFC and STL versions here on codeproject. I used methods from memory, so they may not be precise, but this should do what you want. The added benefit is that the CQStringParser class supports quoted sub-strings. :) CString RemoveDuplicates(CString strSource) { CQStringParser parser(strSource, ','); int nCount = parser.GetCount(); CStringArray strUniques; bool bFound = false; for (int i = 1; i <= nCount; i++) { CString strStart = parser.GetField(i); int nUniqueSize = strUniques.GetSize(); for (int j = 0; j < nUniqueSize; j++) { if (strStart.CompareNoCase(strUniques.GetAt(i)) == 0) { bFound = true; break; } } if (!bFound) { strUniques.Add(strStart); } } parser.RemoveAll(); int nUniqueSize = strUniques.GetSize(); for (int j = 0; j < nUniqueSize; j++) { parser.AddField(strUniques.GetAt(j)); } CString strResult = parser.RebuildOriginalString(); return strResult; }

                              "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
                              -----
                              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                              1 Reply Last reply
                              0
                              • A Andy Brummer

                                :-D Please tell me you just made that up. That isn't an actual example of PE programming, it can't be that would just be absurd.

                                Using the GridView is like trying to explain to someone else how to move a third person's hands in order to tie your shoelaces for you. -Chris Maunder

                                A Offline
                                A Offline
                                amclint
                                wrote on last edited by
                                #17

                                Andy Brummer wrote:

                                Using the GridView is like trying to explain to someone else how to move a third person's hands in order to tie your shoelaces for you. -Chris Maunder

                                ROFL, good sig

                                1 Reply Last reply
                                0
                                • R Rama Krishna Vavilala

                                  In a language of your choice (no PE), implement the following:

                                  string RemoveDuplicates(string csvString) {

                                  }

                                  The function should remove all duplicate values form a string containing comma separated values. RemoveDuplicates("a,b,c,a") => "a,b,c" RemoveDuplicates("a,b,c,a,c,b,c") => "a,b,c" RemoveDuplicates("a,b,c") => "a,b,c" RemoveDuplicates("cat,dog,dog") => "cat,dog" The ideal implementation should have just 1 line of code.


                                  Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

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

                                  In Haskell,

                                  import Data.List
                                  removeDuplicates csvStr = nub (map (delete ',') (groupBy (\a b -> b == ',') csvStr))
                                  

                                  I had to write the 'split on ,' functionality, which takes most of the declaration (it's this bit map (delete ',') (groupBy (\a b -> b == ',') csvStr)), but Haskell handily has a 'remove duplicates from a list' function, nub. [Edit]Whoops - forgot to reconstruct the string (also, didn't cope with multi-char strings)!

                                  import Data.List
                                  removeDuplicates csvStr = concat $ intersperse "," $ nub $ map (delete ',') (groupBy (\a b -> b /= ',') csvStr)
                                  

                                  [/Edit] [Edit 2] And on further investigation of Haskell's libraries, there's a splitRegex function:

                                  import Data.List -- for intersperse, nub
                                  import Text.Regex -- for splitRegex, mkRegex
                                  removeDuplicates csvStr = concat $ intersperse "," $ nub $ splitRegex (mkRegex ",") csvStr
                                  

                                  [/Edit 2]

                                  1 Reply Last reply
                                  0
                                  • J Jon Sagara

                                    Plain English Function Called "Remove Duplicates" with Argument Consisting of Comma Separated Values in a Character String Remove Duplicate Values From The Plain English Function Argument Consisting of Comma Separated Values in a Character String Return The Plain English Function Argument Consisting of Comma Separated Values in a Character String, But With Duplicate Values Removed End Of Plain English Function Called "Remove Duplicates" with Argument Consisting of Comma Separated Values in a Character String Excuse me while I go hurl X|

                                    Jon Sagara When I grow up, I'm changing my name to Joe Kickass! My Site | My Blog | My Articles

                                    C Offline
                                    C Offline
                                    Chris S Kaiser
                                    wrote on last edited by
                                    #19

                                    This just won't ever get old... :laugh::laugh::laugh:

                                    What's in a sig? This statement is false. Build a bridge and get over it. ~ Chris Maunder

                                    1 Reply Last reply
                                    0
                                    • T ToddHileHoffer

                                      In C# string RemoveDuplicates(string csvString) { string[] x = csvString.Split(char.Parse(",")); System.Collections.Specialized.StringCollection c = new System.Collections.Specialized.StringCollection(); foreach (string y in x) { if (!c.Contains(y)) c.Add(y); } string result = ""; foreach (string z in c) { result += z + ","; } return result.Substring(0, result.Length - 1); }


                                      how vital enterprise application are for proactive organizations leveraging collective synergy to think outside the box and formulate their key objectives into a win-win game plan with a quality-driven approach that focuses on empowering key players to drive-up their core competencies and increase expectations with an all-around initiative to drive up the bottom-line. But of course, that's all a "high level" overview of things --thedailywtf 3/21/06

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

                                      public static string RemoveDuplicates ( string Subject ) { System.Text.StringBuilder result = new System.Text.StringBuilder ( Subject.Length ) ; System.Collections.Specialized.StringCollection dic = new System.Collections.Specialized.StringCollection() ; foreach ( string temp in Subject.Split ( new char[] { ',' } , System.StringSplitOptions.None ) ) { if ( !dic.Contains ( temp ) ) { dic.Add ( temp ) ; result.Append ( temp ) ; result.Append ( "," ) ; } } return ( result.Remove ( result.Length-1 , 1 ).ToString() ) ; }

                                      1 Reply Last reply
                                      0
                                      • R Rama Krishna Vavilala

                                        In a language of your choice (no PE), implement the following:

                                        string RemoveDuplicates(string csvString) {

                                        }

                                        The function should remove all duplicate values form a string containing comma separated values. RemoveDuplicates("a,b,c,a") => "a,b,c" RemoveDuplicates("a,b,c,a,c,b,c") => "a,b,c" RemoveDuplicates("a,b,c") => "a,b,c" RemoveDuplicates("cat,dog,dog") => "cat,dog" The ideal implementation should have just 1 line of code.


                                        Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

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

                                        Does it count if we write a class to implement a distinct StringCollection with an appropriate ToString() to do most of the work? Resultant function could be something like: string RemoveDuplicates(string csvString) { return ( (new DistinctStringCollection ( csvString.Split ( new char[] { ',' } ) )).ToString() ) ; }

                                        M 1 Reply Last reply
                                        0
                                        • R Rama Krishna Vavilala

                                          Depends on the language (probably it is better to call 1 statement instead of 1 line): Something like this[^]


                                          Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

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

                                          I just wanted to point out that "lines of code" is not a very worthwhile concept in relation to "modern programming languages".

                                          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