## Code Jam programming competition

11 April, 2010 § 2 Comments

I found out about the Google Code Jam programming competition this past week through Reddit and registered for it. It’s my first programming competition so I’m pretty excited to see how it turns out.

I’ve started to go through the practice problems listed on the site and completing them as though they are part of the competition. As I compete them, I’ll upload my solutions to the problem. Since these solutions are written in haste and I don’t plan on making the code maintainable, for your own benefit please don’t copy the code and use it in an actual production scenario.

The first problem that I have solved is the Alien Numbers problem from April, 2008.

The decimal numeral system is composed of ten digits, which we represent as “0123456789” (the digits in a system are written from lowest to highest). Imagine you have discovered an alien numeral system composed of some number of digits, which may or may not be the same as those used in decimal. For example, if the alien numeral system were represented as “oF8”, then the numbers one through ten would be (F, 8, Fo, FF, F8, 8o, 8F, 88, Foo, FoF). We would like to be able to work with numbers in arbitrary alien systems. More generally, we want to be able to convert an arbitrary number that’s written in one alien system into a second alien system.

See if you can solve this problem. Then continue reading to see my solution.

My solution passes both the small and large test sets:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace CodeJamPracticeCSharp { class Program { static void Main(string[] args) { string input = args[0]; string output = args[1]; File.Delete(output); using (StreamReader reader = new StreamReader(input)) { string firstLine = reader.ReadLine(); int cases; if (int.TryParse(firstLine, out cases)) { for (int i = 0; i < cases; i++) { string[] parts = reader.ReadLine().Split(); string alien_number = parts[0]; string source_language = parts[1]; string target_language = parts[2]; int source_base = source_language.Length; int target_base = target_language.Length; Int64 base_ten_number = 0; var reversed_alien_number = alien_number.Reverse(); for (int j = 0; j < reversed_alien_number.Count(); j++) { char c = reversed_alien_number.ElementAt(j); base_ten_number += source_language.IndexOf(c) * (Int64)Math.Pow(source_base, j); } string reversed_target_number = string.Empty; do { int remainder = (int) base_ten_number % target_base; base_ten_number /= target_base; reversed_target_number += target_language[remainder]; } while (base_ten_number > 0); string target_number = new string(reversed_target_number.Reverse().ToArray()); using (StreamWriter writer = new StreamWriter(output, true)) { writer.WriteLine("Case #{0}: {1}", i + 1, target_number); } } } } } } }

I see you prefer base ten over converting the alien language to good ‘ol binary Keep us informed as to how you do!

I sure will. I’ve been slacking a little on doing the other practice problems. Hopefully when I get this research done for class I’ll have more time.