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
csharpasp-netcomquestionlearning
42 Posts 16 Posters 1 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.
  • D Daniel Grunwald

    Rama Krishna Vavilala wrote:

    No duplicates!

    Does this mean the input files do not contain duplicates, or duplicates need to be removed? In my solution, put a ".Distinct()" between the Aggregate line and the Sort+Output line if you need to remove duplicates.

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

    No your solution is correct! There will be no duplicates.

    Co-Author ASP.NET AJAX in Action

    1 Reply Last reply
    0
    • M Minosknight

      00110001001011000100001101101000011100100110100101110011001000000 10011010110000101110101011011100110010001100101011100100000110100 00101000110110001101010011010100110110001011000100001101101000011 10010011010010111001101110100011010010110000101101110001000000100 01110111001001100001011101010111001100001101000010100011011100110 11100110100001100010010110001001010011011110110100001101110001000 00010100110110100101101101011011010110111101101110011100110010000 00010111100100000011011110111010101110100011011000110000101110111 00100000011100000111001001101111011001110111001001100001011011010 11011010110010101110010000011010000101000110001001101010011001100 11100000110011001011000101001001100001011011010110000100100000010 01011011100100110100101110011011010000110111001100001001000000101 01100110000101110110011010010110110001100001011011000110000100001 10100001010001100100011000000110010001101000011100000101100010011 10011010010111001101101000011000010110111001110100001000000101001 10110100101110110011000010110101101110101011011010110000101110010 00001101000010100011001100110110001110000011000000110011001011000 10011010110000101110010011000110010000001000011011011000110100101 100110011101000110111101101110

      Think of it this way...using a Stradivarius violin to pound nails should not be considered a sound construction technique

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

      Oh, now that's got me thinking of Douglas Adams again, Dirk Gently in fact: The program kgvclsg lgzszsil gvhzxido;vzxdl'vcbx gcb ;klh gjl;ghilsfdghb kZG l gh will perform the task, we just need to find a compiler for it! :laugh:

      M M 2 Replies Last reply
      0
      • D Daniel Grunwald

        using System.Linq;
        class Program {
        public static void Main(string[] args) {
        args.Select(
        fileName => System.IO.File.ReadAllLines(fileName).Select(
        line => new { ID = int.Parse(line.Split(',')[0]), Name = line.Split(',')[1]}
        ))
        .Aggregate((a, b)=>a.Concat(b))
        .OrderBy(a=>a.ID).ToList().ForEach(a=>System.Console.WriteLine("{0},{1}", a.ID, a.Name));
        }
        }

        If you require that the output is written to a file, use this:

        	System.IO.File.WriteAllLines("output.txt", args.Select(
        		fileName => System.IO.File.ReadAllLines(fileName).Select(
        			line => new { ID = int.Parse(line.Split(',')\[0\]), Name = line.Split(',')\[1\]}
        		))
        		.Aggregate((a, b)=>a.Concat(b))
        		.OrderBy(a=>a.ID).Select(a=>string.Format("{0},{1}", a.ID, a.Name)).ToArray());
        

        Last modified: 8mins after originally posted --

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #23

        I need to start learning LINQ :doh: I'll give you a 5 and suppose that it actually works :)

        Luis Alonso Ramos Intelectix Chihuahua, Mexico

        My Blog!

        1 Reply Last reply
        0
        • R Rama Krishna Vavilala

          I expected that someone will come with this kind of solution.;)

          PIEBALDconsult wrote:

          "the numerical value of the id.

          Yes.

          Co-Author ASP.NET AJAX in Action

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

          OK, then my version of sort does. :-D

          1 Reply Last reply
          0
          • C Chris Meech

            Rama Krishna Vavilala wrote:

            Of course it can be done in language of your choice

            Give a couple of hours and I'll have the COBOL version slapped together for you. :)

            Chris Meech I am Canadian. [heard in a local bar]

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

            Watch it; remember the kid-sister rule in the Lounge.


            Software Zen: delete this;

            1 Reply Last reply
            0
            • P PIEBALDconsult

              Oh, now that's got me thinking of Douglas Adams again, Dirk Gently in fact: The program kgvclsg lgzszsil gvhzxido;vzxdl'vcbx gcb ;klh gjl;ghilsfdghb kZG l gh will perform the task, we just need to find a compiler for it! :laugh:

              M Offline
              M Offline
              Minosknight
              wrote on last edited by
              #26

              precisely, I was just being lame and put the desired end result into a text to binary converter. I suppose you could just do it the other way around and TAH-DAH!;P

              Think of it this way...using a Stradivarius violin to pound nails should not be considered a sound construction technique

              1 Reply Last reply
              0
              • D Daniel Grunwald

                using System.Linq;
                class Program {
                public static void Main(string[] args) {
                args.Select(
                fileName => System.IO.File.ReadAllLines(fileName).Select(
                line => new { ID = int.Parse(line.Split(',')[0]), Name = line.Split(',')[1]}
                ))
                .Aggregate((a, b)=>a.Concat(b))
                .OrderBy(a=>a.ID).ToList().ForEach(a=>System.Console.WriteLine("{0},{1}", a.ID, a.Name));
                }
                }

                If you require that the output is written to a file, use this:

                	System.IO.File.WriteAllLines("output.txt", args.Select(
                		fileName => System.IO.File.ReadAllLines(fileName).Select(
                			line => new { ID = int.Parse(line.Split(',')\[0\]), Name = line.Split(',')\[1\]}
                		))
                		.Aggregate((a, b)=>a.Concat(b))
                		.OrderBy(a=>a.ID).Select(a=>string.Format("{0},{1}", a.ID, a.Name)).ToArray());
                

                Last modified: 8mins after originally posted --

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

                My first solution requires loading everything into memory (ReadAllLines/WriteAllLines uses arrays, OrderBy requires having the whole list in memory). Here is another LINQ solution that uses a custom functions for reading/writing files and merging the enumerables:

                using System;
                using System.IO;
                using System.Collections.Generic;
                using System.Linq;
                static class Program {
                public static void Main(string[] args) {
                SortedMerge(args.Select(
                fileName => FileReadLines(fileName).Select(
                line => new { ID = int.Parse(line.Split(',')[0]), Name = line.Split(',')[1]}
                )), a=>a.ID)
                .Select(a=>string.Format("{0},{1}", a.ID, a.Name))
                .WriteToFile("output.txt");
                }

                static IEnumerable<string> FileReadLines(string fileName)
                {
                	using (StreamReader reader = new StreamReader(fileName)) {
                		string line;
                		while ((line = reader.ReadLine()) != null) {
                			Console.WriteLine("read " + line + " from " + fileName);
                			yield return line;
                		}
                	}
                }
                
                static void WriteToFile(this IEnumerable<string> lines, string fileName)
                {
                	using (StreamWriter writer = new StreamWriter(fileName)) {
                		foreach (string line in lines) {
                			Console.WriteLine("write " + line + " to " + fileName);
                			writer.WriteLine(line);
                		}
                	}
                }
                
                static IEnumerable<T> SortedMerge<T, K>(IEnumerable<IEnumerable<T>> inputs, Func<T, K> keySelector) where K : IComparable<K>
                {
                	var enumerators = inputs.Select(o=>o.GetEnumerator()).ToList();
                	var disposables = enumerators.ToList(); // make copy of enumerators for disposing them later
                	try {
                		// move all enumerators on the first element
                		enumerators.RemoveAll(e=>!e.MoveNext());
                		while (enumerators.Count > 0) {
                			int smallest = 0;
                			for (int i = 1; i < enumerators.Count; i++) {
                				// the the element of the current enumerator smaller than the best found so far?
                				if (keySelector(enumerators\[i\].Current).CompareTo(keySelector(enumerators\[smallest\].Current)) < 0) {
                					smallest = i;
                				}
                			}
                			yield return enumerators\[smallest\].Current;
                			if (!enumerators\[smallest\].MoveNext())
                				enumerators.RemoveAt(smallest);
                		}
                	} finally {
                		disposables.ForEach(d => d.Dispose());
                	}
                }
                

                }

                The debug output shows that the program is writing as soon as possible; written lines will be collected by the GC, so this solution can merge multi-GB files without running out of memory. Output:

                C:\temp\SharpDevelop Projects\CPQ

                R 1 Reply Last reply
                0
                • R Rama Krishna Vavilala

                  There are some number of text files. Each file contains a number separated by a comma and a name. e.g. File 1

                  1,Chris Maunder
                  20248,Nishant Sivakumar
                  36803,Marc Clifton
                  ...

                  File 2

                  6556,Christian Graus
                  7741,John Simmons / outlaw programmer
                  15383,Rama Krishna Vavilala
                  ...

                  All the records in the file are sorted by the id (the number). Note there can be many such files. The purpose is to generate a new file like this:

                  1,Chris Maunder
                  6556,Christian Graus
                  7741,John Simmons / outlaw programmer
                  15383,Rama Krishna Vavilala
                  20248,Nishant Sivakumar
                  36803,Marc Clifton
                  ...

                  Of course it can be done in a programming language of your choice. I might be able to hand over a prize for the most innovative and interesting/ solution.;) -- modified at 15:01 Friday 24th August, 2007

                  Co-Author ASP.NET AJAX in Action

                  T Offline
                  T Offline
                  Tarakeshwar Reddy
                  wrote on last edited by
                  #28

                  Dictionary<int, string> cpians = new Dictionary<int, string>();
                  List<int> keylist = new List<int>();

                  string strLine = "";

                  foreach (System.IO.FileInfo fi in new System.IO.DirectoryInfo(@"C:\quiz\").GetFiles())
                  {              
                      using (System.IO.StreamReader sr = fi.OpenText())
                      {
                          while (sr.Peek() >= 0)
                          {
                              strLine = sr.ReadLine();
                              cpians.Add(Int32.Parse(strLine.Split(',')[0]), strLine.Split(',')[1]);
                              keylist.Add(Int32.Parse(strLine.Split(',')[0]));
                          }
                      }
                  }

                  keylist.Sort();

                  using (System.IO.FileStream file = new System.IO.FileStream(@"C:\TotalList.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write))
                  {
                      using (System.IO.StreamWriter sw = new System.IO.StreamWriter(file))
                      {
                          foreach (int key in keylist)
                          {
                              sw.WriteLine(key + "," + cpians[key]);
                          }
                      }
                  }

                  The items in the file need not be sorted for this solution.

                  T 1 Reply Last reply
                  0
                  • R Rama Krishna Vavilala

                    There are some number of text files. Each file contains a number separated by a comma and a name. e.g. File 1

                    1,Chris Maunder
                    20248,Nishant Sivakumar
                    36803,Marc Clifton
                    ...

                    File 2

                    6556,Christian Graus
                    7741,John Simmons / outlaw programmer
                    15383,Rama Krishna Vavilala
                    ...

                    All the records in the file are sorted by the id (the number). Note there can be many such files. The purpose is to generate a new file like this:

                    1,Chris Maunder
                    6556,Christian Graus
                    7741,John Simmons / outlaw programmer
                    15383,Rama Krishna Vavilala
                    20248,Nishant Sivakumar
                    36803,Marc Clifton
                    ...

                    Of course it can be done in a programming language of your choice. I might be able to hand over a prize for the most innovative and interesting/ solution.;) -- modified at 15:01 Friday 24th August, 2007

                    Co-Author ASP.NET AJAX in Action

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

                    string inline ;

                    System.Collections.Generic.List list =
                    new System.Collections.Generic.List() ;

                    while ( ( inline = System.Console.ReadLine() ) != null )
                    {
                    list.Add ( inline.PadLeft ( inline.Length + 16 - inline.IndexOf ( ',' ) , ' ' ) ) ;
                    }

                    list.Sort() ;

                    foreach ( string s in list )
                    {
                    System.Console.WriteLine ( s.TrimStart() ) ;
                    }

                    1 Reply Last reply
                    0
                    • R Rama Krishna Vavilala

                      There are some number of text files. Each file contains a number separated by a comma and a name. e.g. File 1

                      1,Chris Maunder
                      20248,Nishant Sivakumar
                      36803,Marc Clifton
                      ...

                      File 2

                      6556,Christian Graus
                      7741,John Simmons / outlaw programmer
                      15383,Rama Krishna Vavilala
                      ...

                      All the records in the file are sorted by the id (the number). Note there can be many such files. The purpose is to generate a new file like this:

                      1,Chris Maunder
                      6556,Christian Graus
                      7741,John Simmons / outlaw programmer
                      15383,Rama Krishna Vavilala
                      20248,Nishant Sivakumar
                      36803,Marc Clifton
                      ...

                      Of course it can be done in a programming language of your choice. I might be able to hand over a prize for the most innovative and interesting/ solution.;) -- modified at 15:01 Friday 24th August, 2007

                      Co-Author ASP.NET AJAX in Action

                      L Offline
                      L Offline
                      lost in transition
                      wrote on last edited by
                      #30

                      I would like to see someone right a solution in QBasic:) or Pascal:rose:. Make you own front end. Will this work.

                      namespace CP
                      {
                      static class Program
                      {
                      /// /// The main entry point for the application.
                      ///
                      [STAThread]
                      static void Main()
                      {
                      PQuizOTD pqtd = new PQuizOTD();
                      pqtd.ReadFile(@"C:\TestData1.txt");
                      pqtd.ReadFile(@"C:\TestData2.txt");
                      pqtd.ExportToFile();
                      }
                      }
                      class PQuizOTD
                      {
                      private DataTable dt;

                          public PQuizOTD()
                          {
                              dt = new DataTable();
                              dt.Columns.AddRange(
                                          new DataColumn\[\] { 
                                              new DataColumn("ID"), 
                                              new DataColumn("Name") });
                          }
                      
                          public void ReadFile(string FileName)
                          {
                              string line;
                              StreamReader sr = new StreamReader(FileName);
                              while (!sr.EndOfStream)
                              {
                                  line = sr.ReadLine();
                                  int index = line.IndexOf(",");
                                  DataRow dr = dt.NewRow();
                                  dr\[0\] = line.Substring(0, index);
                                  dr\[1\] = line.Substring(index + 1, (line.Length - index - 1));
                                  dt.Rows.Add(dr);
                              }
                          }
                      
                          public void ExportToFile()
                          {
                              dt.DefaultView.Sort = "Name ASC";
                              dt.AcceptChanges();
                              dt = dt.DefaultView.ToTable();
                              StreamWriter sw = new StreamWriter(@"C:\\TestData\_Export.txt");
                              for (int i = 0; i < dt.Rows.Count; i++)
                              {
                                  sw.WriteLine(dt.Rows\[i\]\[0\].ToString() + "," + dt.Rows\[i\]\[1\].ToString());
                              }
                              sw.Close();
                          }
                      }
                      

                      }


                      God Bless, Jason
                      God doesn't believe in atheist but He still loves them.

                      1 Reply Last reply
                      0
                      • R Rama Krishna Vavilala

                        There are some number of text files. Each file contains a number separated by a comma and a name. e.g. File 1

                        1,Chris Maunder
                        20248,Nishant Sivakumar
                        36803,Marc Clifton
                        ...

                        File 2

                        6556,Christian Graus
                        7741,John Simmons / outlaw programmer
                        15383,Rama Krishna Vavilala
                        ...

                        All the records in the file are sorted by the id (the number). Note there can be many such files. The purpose is to generate a new file like this:

                        1,Chris Maunder
                        6556,Christian Graus
                        7741,John Simmons / outlaw programmer
                        15383,Rama Krishna Vavilala
                        20248,Nishant Sivakumar
                        36803,Marc Clifton
                        ...

                        Of course it can be done in a programming language of your choice. I might be able to hand over a prize for the most innovative and interesting/ solution.;) -- modified at 15:01 Friday 24th August, 2007

                        Co-Author ASP.NET AJAX in Action

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #31

                        Looks like a job for merge sort.


                        "A good athlete is the result of a good and worthy opponent." - David Crow

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        D 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          type *.in | sort > CP.out Unless, by "(the number)", you mean "the numerical value of the id".

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #32

                          Are you sure that TYPE works with wildcards?


                          "A good athlete is the result of a good and worthy opponent." - David Crow

                          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                          P 1 Reply Last reply
                          0
                          • T Tarakeshwar Reddy

                            Dictionary<int, string> cpians = new Dictionary<int, string>();
                            List<int> keylist = new List<int>();

                            string strLine = "";

                            foreach (System.IO.FileInfo fi in new System.IO.DirectoryInfo(@"C:\quiz\").GetFiles())
                            {              
                                using (System.IO.StreamReader sr = fi.OpenText())
                                {
                                    while (sr.Peek() >= 0)
                                    {
                                        strLine = sr.ReadLine();
                                        cpians.Add(Int32.Parse(strLine.Split(',')[0]), strLine.Split(',')[1]);
                                        keylist.Add(Int32.Parse(strLine.Split(',')[0]));
                                    }
                                }
                            }

                            keylist.Sort();

                            using (System.IO.FileStream file = new System.IO.FileStream(@"C:\TotalList.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write))
                            {
                                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(file))
                                {
                                    foreach (int key in keylist)
                                    {
                                        sw.WriteLine(key + "," + cpians[key]);
                                    }
                                }
                            }

                            The items in the file need not be sorted for this solution.

                            T Offline
                            T Offline
                            Tarakeshwar Reddy
                            wrote on last edited by
                            #33

                            Modified version using SortedList<>.

                            SortedList<int, string> cpians = new SortedList<int, string>();                       
                            string strLine = "";

                            foreach (System.IO.FileInfo fi in new System.IO.DirectoryInfo(@"C:\quiz\").GetFiles())
                            {
                                using (System.IO.StreamReader sr = fi.OpenText())
                                {
                                    while (sr.Peek() >= 0)
                                    {
                                        strLine = sr.ReadLine();
                                        cpians.Add(Int32.Parse(strLine.Split(',')[0]), strLine.Split(',')[1]);                       
                                    }
                                }
                            }

                            using (System.IO.FileStream file = new System.IO.FileStream(@"C:\TotalList.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write))
                            {
                                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(file))
                                {
                                    foreach (KeyValuePair<int, string> kvp in cpians)
                                    {
                                        sw.WriteLine(kvp.Key + "," + kvp.Value);
                                    }
                                }
                            }

                            R 1 Reply Last reply
                            0
                            • D David Crow

                              Looks like a job for merge sort.


                              "A good athlete is the result of a good and worthy opponent." - David Crow

                              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

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

                              Take a look at my "on-the-fly" solution for a merge sort solution that requires memory only linear to the number of files, independent from the file size.

                              1 Reply Last reply
                              0
                              • J Jim Crafton

                                "Computer" I shout. "Yes Master?" "Create a file for me!" "All right. What do you want in the file?" "Names, and an ID number. Can you do that brainiac?" "Cut the crap Boss. Sure thing. Anything else?" "Yeah, keep the file sorted by ID, in ascending order." "Fine. Done. Where do I get the names from?" "They are in another file, it's text, and comma delimited. The first thing on a line is a number, the ID, followed by a name." "Are there multiple entries in a file?" "Yep." "OK, ready to rock and roll."

                                ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! VCF Blog

                                M Offline
                                M Offline
                                martin_hughes
                                wrote on last edited by
                                #35

                                I think I need to adopt this style of programming :)

                                "It was the day before today.... I remember it like it was yesterday." -Moleman

                                1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  Oh, now that's got me thinking of Douglas Adams again, Dirk Gently in fact: The program kgvclsg lgzszsil gvhzxido;vzxdl'vcbx gcb ;klh gjl;ghilsfdghb kZG l gh will perform the task, we just need to find a compiler for it! :laugh:

                                  M Offline
                                  M Offline
                                  martin_hughes
                                  wrote on last edited by
                                  #36

                                  Surely you'd only get a suffusion of yellow given the inputs?

                                  "It was the day before today.... I remember it like it was yesterday." -Moleman

                                  1 Reply Last reply
                                  0
                                  • T Tarakeshwar Reddy

                                    Modified version using SortedList<>.

                                    SortedList<int, string> cpians = new SortedList<int, string>();                       
                                    string strLine = "";

                                    foreach (System.IO.FileInfo fi in new System.IO.DirectoryInfo(@"C:\quiz\").GetFiles())
                                    {
                                        using (System.IO.StreamReader sr = fi.OpenText())
                                        {
                                            while (sr.Peek() >= 0)
                                            {
                                                strLine = sr.ReadLine();
                                                cpians.Add(Int32.Parse(strLine.Split(',')[0]), strLine.Split(',')[1]);                       
                                            }
                                        }
                                    }

                                    using (System.IO.FileStream file = new System.IO.FileStream(@"C:\TotalList.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write))
                                    {
                                        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(file))
                                        {
                                            foreach (KeyValuePair<int, string> kvp in cpians)
                                            {
                                                sw.WriteLine(kvp.Key + "," + kvp.Value);
                                            }
                                        }
                                    }

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

                                    Infact you don't even need fi.OpenText. You can directly supply the file names to the StreamReader and StreamWriter.

                                    Co-Author ASP.NET AJAX in Action

                                    T 1 Reply Last reply
                                    0
                                    • D David Crow

                                      Are you sure that TYPE works with wildcards?


                                      "A good athlete is the result of a good and worthy opponent." - David Crow

                                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

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

                                      Yes, it works.

                                      1 Reply Last reply
                                      0
                                      • R Rama Krishna Vavilala

                                        Infact you don't even need fi.OpenText. You can directly supply the file names to the StreamReader and StreamWriter.

                                        Co-Author ASP.NET AJAX in Action

                                        T Offline
                                        T Offline
                                        Tarakeshwar Reddy
                                        wrote on last edited by
                                        #39

                                        Oh yeah, for the StreamWriter, I could have directly given the filename :doh:. In the StreamReader, I use FileInfo to get a list of files in the particular directory, since I have it declared I am using fi.OpenText() which returns to me a StreamReader

                                        1 Reply Last reply
                                        0
                                        • D Daniel Grunwald

                                          My first solution requires loading everything into memory (ReadAllLines/WriteAllLines uses arrays, OrderBy requires having the whole list in memory). Here is another LINQ solution that uses a custom functions for reading/writing files and merging the enumerables:

                                          using System;
                                          using System.IO;
                                          using System.Collections.Generic;
                                          using System.Linq;
                                          static class Program {
                                          public static void Main(string[] args) {
                                          SortedMerge(args.Select(
                                          fileName => FileReadLines(fileName).Select(
                                          line => new { ID = int.Parse(line.Split(',')[0]), Name = line.Split(',')[1]}
                                          )), a=>a.ID)
                                          .Select(a=>string.Format("{0},{1}", a.ID, a.Name))
                                          .WriteToFile("output.txt");
                                          }

                                          static IEnumerable<string> FileReadLines(string fileName)
                                          {
                                          	using (StreamReader reader = new StreamReader(fileName)) {
                                          		string line;
                                          		while ((line = reader.ReadLine()) != null) {
                                          			Console.WriteLine("read " + line + " from " + fileName);
                                          			yield return line;
                                          		}
                                          	}
                                          }
                                          
                                          static void WriteToFile(this IEnumerable<string> lines, string fileName)
                                          {
                                          	using (StreamWriter writer = new StreamWriter(fileName)) {
                                          		foreach (string line in lines) {
                                          			Console.WriteLine("write " + line + " to " + fileName);
                                          			writer.WriteLine(line);
                                          		}
                                          	}
                                          }
                                          
                                          static IEnumerable<T> SortedMerge<T, K>(IEnumerable<IEnumerable<T>> inputs, Func<T, K> keySelector) where K : IComparable<K>
                                          {
                                          	var enumerators = inputs.Select(o=>o.GetEnumerator()).ToList();
                                          	var disposables = enumerators.ToList(); // make copy of enumerators for disposing them later
                                          	try {
                                          		// move all enumerators on the first element
                                          		enumerators.RemoveAll(e=>!e.MoveNext());
                                          		while (enumerators.Count > 0) {
                                          			int smallest = 0;
                                          			for (int i = 1; i < enumerators.Count; i++) {
                                          				// the the element of the current enumerator smaller than the best found so far?
                                          				if (keySelector(enumerators\[i\].Current).CompareTo(keySelector(enumerators\[smallest\].Current)) < 0) {
                                          					smallest = i;
                                          				}
                                          			}
                                          			yield return enumerators\[smallest\].Current;
                                          			if (!enumerators\[smallest\].MoveNext())
                                          				enumerators.RemoveAt(smallest);
                                          		}
                                          	} finally {
                                          		disposables.ForEach(d => d.Dispose());
                                          	}
                                          }
                                          

                                          }

                                          The debug output shows that the program is writing as soon as possible; written lines will be collected by the GC, so this solution can merge multi-GB files without running out of memory. Output:

                                          C:\temp\SharpDevelop Projects\CPQ

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

                                          I guess you are the winner. Will you like a copy of ASP.NET AJAX in Action?

                                          Co-Author ASP.NET AJAX in Action

                                          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