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.
  • 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
    Shog9 0
    wrote on last edited by
    #23

    function Reduce(str)
    {
    var ret = new Array();
    var a = str.split(',');
    for (var i in a)
    {
    if ( !ret[a[i]] ) ret.push(a[i]);
    ret[a[i]] = true;
    }
    return ret.join(',');
    }

    Or, if you can use 1.7:

    function Reduce(str)
    {
    function Unique(a)
    {
    var o = {};
    for each (var i in a)
    {
    if (!o[i]) yield i;
    o[i] = true;
    }
    }

    return [i for (i in Unique(str.split(',')))].join(',');
    }

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

      D Offline
      D Offline
      David Stone
      wrote on last edited by
      #24

      Daniel Grunwald wrote:

      Not that anyone would store large amounts of data in CSV strings....

      :laugh: How optimistic. :rolleyes:

      Once you wanted revolution
      Now you're the institution
      How's it feel to be the man?

      1 Reply 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
        Matt Gerrans
        wrote on last edited by
        #25

        Similarly in Python: def RemoveDuplicates(text): return {}.fromkeys( [elem.strip() for elem in text.split(',')] ).keys() (But I also stripped the spaces after splitting on commas, to allow "a, b, b,c,c,d" kind of stuff).

        Matt Gerrans

        1 Reply Last reply
        0
        • C Chris Losinger

          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 Offline
          J Offline
          Jorgen Sigvardsson
          wrote on last edited by
          #26

          Boostified:

          std::string fn(std::string in)
          {
          std::string out;

          typedef boost::tokenizer<boost::char\_separator<char> > tokenizer;
          tokenizer foo(in, boost::char\_separator<char>(",");
          
          tokenizer::iterator it = foo.begin(), end = foo.end();
          while(it != end)
          {
              out += \*it++;
              if(it != end)
                  out += ",";
          }
          return out; 
          

          }

          There's probably a boost function somewhere which allows one to join strings as well, but I didn't bother to look. :)

          -- Not based on the Novel by James Fenimore Cooper

          C 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

            A Offline
            A Offline
            Andre xxxxxxx
            wrote on last edited by
            #27

            print RemoveDuplicates("a,b,b,c,b,c");

            sub RemoveDuplicates
            {
            foreach (split (/,/,$_[0])) { $_{$_} = $_; }
            join (",",keys %_);
            }

            J M 2 Replies Last reply
            0
            • J Jorgen Sigvardsson

              Boostified:

              std::string fn(std::string in)
              {
              std::string out;

              typedef boost::tokenizer<boost::char\_separator<char> > tokenizer;
              tokenizer foo(in, boost::char\_separator<char>(",");
              
              tokenizer::iterator it = foo.begin(), end = foo.end();
              while(it != end)
              {
                  out += \*it++;
                  if(it != end)
                      out += ",";
              }
              return out; 
              

              }

              There's probably a boost function somewhere which allows one to join strings as well, but I didn't bother to look. :)

              -- Not based on the Novel by James Fenimore Cooper

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

              someday i'll try to figure out how to use boost again. the last time i tried, it was a total installation, dependency, compiler configuration nightmare. definitely not the kind of thing i wanted to get into, just to use their regexp classes.

              image processing | blogging

              J 1 Reply Last reply
              0
              • C Chris Losinger

                someday i'll try to figure out how to use boost again. the last time i tried, it was a total installation, dependency, compiler configuration nightmare. definitely not the kind of thing i wanted to get into, just to use their regexp classes.

                image processing | blogging

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #29

                I've had no problems with the releases since 1.30. This has been with VS 2k3 - don't know what'll happen with VC6 or 2k5.

                -- -= Proudly Made on Earth =-

                1 Reply Last reply
                0
                • J Jorgen Sigvardsson

                  Boostified:

                  std::string fn(std::string in)
                  {
                  std::string out;

                  typedef boost::tokenizer<boost::char\_separator<char> > tokenizer;
                  tokenizer foo(in, boost::char\_separator<char>(",");
                  
                  tokenizer::iterator it = foo.begin(), end = foo.end();
                  while(it != end)
                  {
                      out += \*it++;
                      if(it != end)
                          out += ",";
                  }
                  return out; 
                  

                  }

                  There's probably a boost function somewhere which allows one to join strings as well, but I didn't bother to look. :)

                  -- Not based on the Novel by James Fenimore Cooper

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

                  Joergen Sigvardsson wrote:

                  *it++; if(it != end)

                  ah. nice touch.

                  image processing | blogging

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    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 Offline
                    M Offline
                    Marc 0
                    wrote on last edited by
                    #31

                    Yeah piece of cake: return (new [UniqueStringList](http://www.codeproject.com/csharp/uniquestringlist.asp)[[^](http://www.codeproject.com/csharp/uniquestringlist.asp)](csvString.Split(new char[] {','})).ToString("", ",", ""); It is kind of cheating though ;P


                    "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick


                    || Fold With Us! || Pensieve || VG.Net ||

                    P 1 Reply Last reply
                    0
                    • A Andre xxxxxxx

                      print RemoveDuplicates("a,b,b,c,b,c");

                      sub RemoveDuplicates
                      {
                      foreach (split (/,/,$_[0])) { $_{$_} = $_; }
                      join (",",keys %_);
                      }

                      J Offline
                      J Offline
                      Jorgen Sigvardsson
                      wrote on last edited by
                      #32

                      Ah.. Perl syntax. Gives me the shiver every time. ;)

                      -- Now with chucklelin

                      1 Reply Last reply
                      0
                      • M Marc 0

                        Yeah piece of cake: return (new [UniqueStringList](http://www.codeproject.com/csharp/uniquestringlist.asp)[[^](http://www.codeproject.com/csharp/uniquestringlist.asp)](csvString.Split(new char[] {','})).ToString("", ",", ""); It is kind of cheating though ;P


                        "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick


                        || Fold With Us! || Pensieve || VG.Net ||

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

                        Ah, very good, I'll have to take a deeper look at UniqueStringList. Here's what I just whipped up: public partial class DistinctStringCollection : System.Collections.Specialized.StringCollection { public static DistinctStringCollection FromCSV ( string CSV ) { DistinctStringCollection result = new DistinctStringCollection() ; foreach ( string temp in CSV.Trim ( new char[] { ',' } ).Split ( new char[] { ',' } ) ) { if ( !result.Contains ( temp ) ) { result.Add ( temp ) ; } } return ( result ) ; } public string ToCSV ( ) { System.Text.StringBuilder result = new System.Text.StringBuilder() ; foreach ( string temp in this ) { result.Append ( temp ) ; result.Append ( "," ) ; } return ( result.Remove ( result.Length-1 , 1 ).ToString() ) ; } } And then... public static string RemoveDuplicates ( string Subject ) { return ( DistinctStringCollection.FromCSV ( Subject ).ToCSV() ) ; }

                        P 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

                          M Offline
                          M Offline
                          Michael Dunn
                          wrote on last edited by
                          #34

                          Perl (not tested, may or may not work) ;) print join ( grep { ++$tokens{$_} == 1 } split(/,/), ',' )

                          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                          1 Reply Last reply
                          0
                          • A Andre xxxxxxx

                            print RemoveDuplicates("a,b,b,c,b,c");

                            sub RemoveDuplicates
                            {
                            foreach (split (/,/,$_[0])) { $_{$_} = $_; }
                            join (",",keys %_);
                            }

                            M Offline
                            M Offline
                            Michael Dunn
                            wrote on last edited by
                            #35

                            Does keys return the keys in the same order as they were inserted? When I did my solution below, I consciously kept the order of the words the same in the output.

                            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                            A 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

                              E Offline
                              E Offline
                              Eytukan
                              wrote on last edited by
                              #36

                              It's a becoming a bit of addiction to wait for friday programming post... :) hmm but I'm simply watching it now. I'll start posting my version soon :). Very nice Rama :).. in particular everytime I look for Nish to responding with his code :-D...that;s cool. Also at the end you can post your own answer right?


                              :Gong: 歡迎光臨 吐 西批 :Gong:

                              1 Reply Last reply
                              0
                              • M Michael Dunn

                                Does keys return the keys in the same order as they were inserted? When I did my solution below, I consciously kept the order of the words the same in the output.

                                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                                A Offline
                                A Offline
                                Andre xxxxxxx
                                wrote on last edited by
                                #37

                                Michael Dunn wrote:

                                Does keys return the keys in the same order as they were inserted?

                                From the perldoc: "The keys are returned in an apparently random order. The actual random order is subject to change in future versions of perl. Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons." But that was not an requirement ;P

                                1 Reply Last reply
                                0
                                • R Rama Krishna Vavilala

                                  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 Offline
                                  E Offline
                                  Eytukan
                                  wrote on last edited by
                                  #38

                                  but your 4th example has already said that. ;)


                                  :Gong: 歡迎光臨 吐 西批 :Gong:

                                  1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    Ah, very good, I'll have to take a deeper look at UniqueStringList. Here's what I just whipped up: public partial class DistinctStringCollection : System.Collections.Specialized.StringCollection { public static DistinctStringCollection FromCSV ( string CSV ) { DistinctStringCollection result = new DistinctStringCollection() ; foreach ( string temp in CSV.Trim ( new char[] { ',' } ).Split ( new char[] { ',' } ) ) { if ( !result.Contains ( temp ) ) { result.Add ( temp ) ; } } return ( result ) ; } public string ToCSV ( ) { System.Text.StringBuilder result = new System.Text.StringBuilder() ; foreach ( string temp in this ) { result.Append ( temp ) ; result.Append ( "," ) ; } return ( result.Remove ( result.Length-1 , 1 ).ToString() ) ; } } And then... public static string RemoveDuplicates ( string Subject ) { return ( DistinctStringCollection.FromCSV ( Subject ).ToCSV() ) ; }

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

                                    Well at least I had something about which to think over the weekend. I've decided that implementing a DistinctStringCollection (or UniqueStringList) isn't very worthwhile (to me). (Nor is writing a RemoveDuplicates that only works on strings.) Having a method to make a CSV from any IEnumerable is: public static string CSVify ( System.Collections.IEnumerable Subject ) { System.Text.StringBuilder result = new System.Text.StringBuilder() ; foreach ( object temp in Subject ) { if ( temp != null ) { result.Append ( temp.ToString() ) ; result.Append ( "," ) ; } } return ( result.Remove ( result.Length-1 , 1 ).ToString() ) ; } Having methods to remove duplicates from (nearly?) any array or IList is: public static T[] RemoveDuplicates ( T[] Subject ) { T[] result = new T [ Subject.Length ] ; foreach ( T temp in Subject ) { for ( int index = 0 ; index < result.Length ; index++ ) { if ( result [ index ] == null ) { result [ index ] = temp ; break ; } else { if ( temp.Equals ( result [ index ] ) ) { break ; } } } } return ( result ) ; } public static T RemoveDuplicates ( T Subject ) where T : System.Collections.IList , new() { T result = new T() ; foreach ( object temp in Subject ) { if ( !result.Contains ( temp ) ) { result.Add ( temp ) ; } } return ( result ) ; } A string-only RemoveDuplicates can then be written as: public static string RemoveDuplicates ( string Subject ) { return ( CSVify ( RemoveDuplicates ( Subject.Split ( new char[] {

                                    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