Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 03-26-2013, 11:24 PM   #1
..m.0,0.m..
Site Team
 
iPwn's Avatar
 
Join Date: May 2010
Location: USA
Posts: 3,870
Default Applied for a .Net developer job

I know... out of my norm, but check out my 'test.'

Here's the code I was given...
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
//Sample Code
//C# Programmer Question

//Below is a class with one function that uses a pipe-delimited file that is updated once a week.  The class compiles and works.  In fact, it has been in production for several weeks, is used by other applications, and no one has complained.  However, your manager has asked you to modify the code to make it more maintainable / readable.


//Pipe-delimited file sample:
//vaughan|life by the drop
//hendrix|red house
//lennon|let it be
//clapton|layla
//jagger|rocks off
//cobain|all apologies
//simon|me and julio down by the school yard
//morrison|
//joplin|me and bobby mcgee


using System.IO;
static class Module1 {
    public static void Main() {
        OutputSongFromDeadArtistsAlphabetically();
    }

    public static void OutputSongFromDeadArtistsAlphabetically() {
        string results = null;
        try {
            StreamReader sr = new StreamReader(File.Open("..\\..\\input.txt", System.IO.FileMode.Open));
            string y = null;
            ArrayList z = new ArrayList();
            string leftHalf = null;
            string rightHalf = null;
            int pos = 0;
            while (!sr.EndOfStream) {
                y = sr.ReadLine();
                pos = y.IndexOf("|");
                leftHalf = y.Substring(0, pos);
                rightHalf = y.Substring(pos + 1, y.Length - pos - 1);
                if (leftHalf == "vaughan") {
                    if (!string.IsNullOrEmpty(rightHalf)) {
                        z.Add(rightHalf);
                    }
                }
                if (leftHalf == "hendrix") {
                    if (!string.IsNullOrEmpty(rightHalf)) {
                        z.Add(rightHalf);
                    }
                }
                if (leftHalf == "lennon") {
                    if (!string.IsNullOrEmpty(rightHalf)) {
                        z.Add(rightHalf);
                    }
                }
                if (leftHalf == "cobain") {
                    if (!string.IsNullOrEmpty(rightHalf)) {
                        z.Add(rightHalf);
                    }
                }
                if (leftHalf == "joplin") {
                    if (!string.IsNullOrEmpty(rightHalf)) {
                        z.Add(rightHalf);
                    }
                }
                if (leftHalf == "morrison") {
                    if (!string.IsNullOrEmpty(rightHalf)) {
                        z.Add(rightHalf);
                    }
                }
            }

            //sort the list
            string temp = null;
            for (int i = 0; i <= z.Count - 1; i++) {
                for (int j = z.Count - 1; j >= i + 1; j += -1) {
                    if (String.Compare(z[j].ToString(), z[j - 1].ToString(), true) < 0) {
                        temp = z[j].ToString();
                        z[j] = z[j - 1];
                        z[j - 1] = temp;
                    }
                }
            }

            //print the sorted output
            foreach (string c in z) {
                Console.WriteLine(c);
            }
            sr.Close();
        }
        catch (Exception e) {
            throw e;
        }
    }
}
And here's what I turned in as a 'restructure.'

Code:
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;

static class Module1
{
    public static void Main()
    {
        string[] Artist = { "vaughan", "hendrix", "lennon", "clapton", "jagger", "cobain", "simon", "morrison", "joplin" };
        ArrayList Songs = new ArrayList();
        DataTable Tbl = new DataTable();
        Tbl.Columns.Add("Artist");
        Tbl.Columns.Add("Song");

        try
        {            
            StreamReader SR = new StreamReader("DeadArtists.txt");

            for (int i = 0; !SR.EndOfStream; i++)
            {
                string[] cells = SR.ReadLine().Split('|');
                Tbl.Rows.Add(cells);
            }
        }
        catch (Exception e)
        { throw e; }

        for (int i = 0; i < Tbl.Rows.Count; i++)
        {
            object o = Tbl.Rows[i]["Artist"];
            string temp = o.ToString();

            if (Artist.Contains(temp) == true)
                {
                    object s = Tbl.Rows[i]["Song"];
                    if (s.ToString() != "")
                    {
                        Songs.Add(s.ToString());
                    }
                }
        }

        Songs.Sort();
        foreach (string c in Songs)
        {
            Console.WriteLine(c);
        }
        Console.ReadLine();
    }
}
Pretty excited to see how it turns out.

Edit: How the interview turns out... the code works
__________________

__________________
Me: You'd think as the dominant species we wouldn't be so effing stupid.
J: We're just intelligent enough to be completely effing stupid.
iPwn is offline   Reply With Quote
Old 03-27-2013, 08:23 AM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Applied for a .Net developer job

One thing worth bearing in mind for the future is that (in C# at least) the convention is that all variable names are camelCased, not PascalCased (or other.) In laymans terms that means they should start with a lower cased letter.

Shouldn't be a deal breaker, just remember in these sorts of interviews to closely follow the language conventions
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 05:58 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0