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 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 Offline
    R Offline
    Rama Krishna Vavilala
    wrote on last edited by
    #1

    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 J C L E 14 Replies 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
      #2

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

      R D 2 Replies 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

        J Offline
        J Offline
        Jim Crafton
        wrote on last edited by
        #3

        "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

        R M P 3 Replies Last reply
        0
        • P PIEBALDconsult

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

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

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

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

            Is it a variation of Osmo's Plain English.

            Co-Author ASP.NET AJAX in Action

            J 1 Reply Last reply
            0
            • R Rama Krishna Vavilala

              Is it a variation of Osmo's Plain English.

              Co-Author ASP.NET AJAX in Action

              J Offline
              J Offline
              Jim Crafton
              wrote on last edited by
              #6

              No, but I realized after a few lines that's what it might look like. :)

              ¡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

              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

                C Offline
                C Offline
                Chris Meech
                wrote on last edited by
                #7

                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]

                R G A 3 Replies 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]

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

                  You have all the time:)

                  Co-Author ASP.NET AJAX in Action

                  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
                    Luis Alonso Ramos
                    wrote on last edited by
                    #9

                    In English.NET? :)

                    create a new List and Hashtable
                     
                    foreach file
                    {
                       open file
                     
                       while(i can read from file)
                       {
                          read a line
                          split it by the comma
                          add the id to the list
                          add the id and name to the hashtable (key, value)
                       }
                     
                       close file
                    }
                     
                    list.Sort();
                     
                    open result file
                     
                    foreach id in the list
                    {
                       get the name from the hashtable using the id
                       write to file(String.Format("{0},{1}", id, name);
                    }
                     
                    close result file
                    

                    :)

                    Luis Alonso Ramos Intelectix Chihuahua, Mexico

                    My Blog!

                    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

                      E Offline
                      E Offline
                      El Corazon
                      wrote on last edited by
                      #10

                      Rama Krishna Vavilala wrote:

                      Of course it can be done in language of your choice.

                      too lazy to write you code.... but I had this laying in my bookmarks. how about a bitonic merge sort on the GPU.... http://www.cis.upenn.edu/~suvenkat/700/lectures/19/sorting-kider.pdf[^]

                      _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

                      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
                        ToddHileHoffer
                        wrote on last edited by
                        #11

                        I wrote an application to do this. Front end and all in 7 minutes. I could have used a hash-table or arraylist or whatever but I used ado.net because that's the kind of guy I am. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { DataTable dt; Int32 FilesRead = 0; private void AddFileToDataTable(System.IO.Stream fileStream) { System.IO.StreamReader sr = new System.IO.StreamReader(fileStream); string input; DataRow dr; while ((input = sr.ReadLine()) != null) { string[] s = input.Split(new char[] { ',' }); dr = dt.NewRow(); dr["ID"] = int.Parse(s[0]); dr["Name"] = s[1]; dt.Rows.Add(dr); } sr.Close(); } public Form1() { InitializeComponent(); dt = new DataTable(); DataColumn dc; dc = new DataColumn(); dc.DataType = System.Type.GetType("System.Int32"); dc.ColumnName = "ID"; dt.Columns.Add(dc); dc = new DataColumn(); dc.DataType = System.Type.GetType("System.String"); dc.ColumnName = "Name"; dt.Columns.Add(dc); } private void button1_Click(object sender, EventArgs e) { this.openFileDialog1.ShowDialog(); System.IO.Stream sr = System.IO.File.Open(this.openFileDialog1.FileName, System.IO.FileMode.Open); AddFileToDataTable(sr); FilesRead += 1; this.label1.Text = "Files Read:" + FilesRead.ToString(); } private void button2_Click(object sender, EventArgs e) { this.saveFileDialog1.ShowDialog(); DataView dv = new DataView(dt); dv.Sort = "ID ASC"; dt = dv.ToTable(); StringBuilder sb = new StringBuilder(); foreach (DataRow r in dt.Rows) { sb.Append(r[0].ToString() + "," + r[1].ToString() + Environment.NewLine); } System.IO.File.WriteAllText(this.saveFileDialog1.FileName, sb.ToString()); } } }

                        <

                        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
                          Daniel Grunwald
                          wrote on last edited by
                          #12

                          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 D 2 Replies 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
                            #13

                            What about duplicates?

                            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

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

                              this one feels too much like actual work

                              image processing toolkits | batch image processing

                              L R 2 Replies Last reply
                              0
                              • P PIEBALDconsult

                                What about duplicates?

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

                                There are no duplicates. Assume unique values

                                Co-Author ASP.NET AJAX in Action

                                P D 2 Replies 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

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

                                  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 1 Reply Last reply
                                  0
                                  • C Chris Losinger

                                    this one feels too much like actual work

                                    image processing toolkits | batch image processing

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

                                    Can anyone do this help me do this for my homework? It's due in two hours and I've had all the semester!! Please help urgent!! ;P

                                    Luis Alonso Ramos Intelectix Chihuahua, Mexico

                                    My Blog!

                                    1 Reply Last reply
                                    0
                                    • R Rama Krishna Vavilala

                                      There are no duplicates. Assume unique values

                                      Co-Author ASP.NET AJAX in Action

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

                                      Rama Krishna Vavilala wrote:

                                      Assume

                                      That's a naughty word.

                                      1 Reply Last reply
                                      0
                                      • R Rama Krishna Vavilala

                                        There are no duplicates. Assume unique values

                                        Co-Author ASP.NET AJAX in Action

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

                                        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 1 Reply Last reply
                                        0
                                        • C Chris Losinger

                                          this one feels too much like actual work

                                          image processing toolkits | batch image processing

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

                                          Yes! It is close. All the quizzes are based on actual work I did. It is based on a IIS log analyzer utility I once wrote which sorted log records from different servers into one file. Except that the actual program was far more complex as it had to read large files in buffers and merge them.

                                          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