Google CodeJam 2013 Preparation

UPDATE:See how I performed in Google Code Jam 2013 Qualifying

I’ve decided to participate in Google Code Jam 2013! I signed up a couple of weeks ago and had started to look at the past problems in preparation.

What Is Code Jam?
For those of you who don’t know what Code Jam is, it is basically a coding competition. There are rounds that you compete in online, completing problems and submitting solutions and output files. If you make it through the rounds, you reach a live final at Google HQ.

Example Problem – “Speaking in tongues”
I’ll post the code which I came up with for a problem called “Speaking in tongues” which was only the first problem in the qualification round of 2012 (so basically the easiest problem that year).
Problem

I’ve chosen to write the solutions in C# which is my strongest language.

For this problem I basically had to figure out the key to a substitution cypher from the text that was provided as an example. Once we have the key then we can use it to decrypt the input text given and create an output file in the correct format.

class Program
    {
        //Dictionary to store the mapping of the key
        private static Dictionary<char, char> map = new Dictionary<char, char>();

        static void Main(string[] args)
        {
            //These strings were found by looking at the example given in the problem
            string a = "abcdefghijklmnopqrstuvwxyz";
            string b = "yhesocvxduiglbkrztnwjpfmaq";

            string output="";
            //build the dictionary up initially so we can use it easily
            for (int i = 0; i < a.Length; i++)
            {
                map.Add(a[i], b[i]);
            }
            //get all the input lines
            string[] lines = File.ReadAllLines(args[0]);
            int t = int.Parse(lines[0]);
            
            //process every line in the file, creating the proper output format
            for (int i = 1; i < lines.Count(); i++)
            {
                output += "Case #" + i + ": " + ConvertFromGooglerese(lines[i]) + Environment.NewLine;
            }


            Console.WriteLine(output);
            File.WriteAllText("output.txt", output);
            Console.ReadLine();

            

        }

        private static string ConvertFromGooglerese(string line)
        {
            string converted = "";
            
            //loop through each character in the line
            foreach (char c in line.ToCharArray())
            {
                //check if the char is in the dictionary
                if (map.ContainsKey(c))
                {
                    //if it is, build up the new string with the converted value
                    char x;
                    map.TryGetValue(c, out x);
                    converted += x.ToString();
                }
                else
                {
                    converted += " ";
                } 
            }

            return converted;
        }

        
    }

What do I expect to get from this?
Ok, well I think my solution was fairly decent. I’ve seen a lot shorter solutions from some of the top guys but this still produces the correct results! However, if I can make it through the qualifying I’d be more than happy.

I think Code Jam is one of those things that helps us developers feel motivated and inspired to do more and do it well. Some of the worlds top competitive coders will be taking part and I’ll be in the same competition, even if its not for a long time!

I hope that at the least it will improve my programming and problem solving skills. Watch this space, I’ll let you know how my qualifying round goes 🙂

Leave a Reply