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 🙂

DevWeek 2013 – My Experience

I was fortunate and lucky enough that my company paid for me to attend DevWeek 2013 for 2/3 of the conference days. I was really excited about going to an event specially for developers. I know it’s cliche, but I was like a kid in a toy shop when I first walked in. There were stalls set up giving out free stuff and information about their products. Oh and a prize draw to be entered at every one for a Surface Tablet! Obviously I set my sights on going around and collecting a memento from each stall. I quickly realised though how all of these things can help me and my company go forward. Serious head was screwed on very quickly.

The day before I had set myself an itinerary of what I wanted to attend. Here was my list:

Day 1 (Wed 6th March)
0930 – Windows 8 UI Design Principles (Dave Wheeler / Mark Smith)
1130 – SQL Server Query Tuning (Klaus Aschenbrenner)
1400 – Securing (ASP.Net) Web API based architectures (Dominick Baier)
1600 – Data Driven Windows 8 Apps (Dave Wheeler / Mark Smith)

Day 2 (Thurs 7th March)
0930 – Design for Testing (Kevin Jones)
1130 – Principles of Programming (Allen Holub)
1400 – Emergent Design (Neal Ford)
1600 – Windows 8 Contracts & Sharing (Mark Smith)

My colleague had recommended that I go see Dave Wheeler’s session to begin with as he was covering some windows 8 stuff and it should be interesting. Straight away though I felt sorry for the guy who was actually there, Mark Smith. He was covering as Dave had injured himself and could not attend (Hope he is better now!). Mark told us he was doing a session in every slot for the week! Over the days I went to a few of Mark’s sessions as I found the first one very good. He kept us interested and mixed it up well with live demos and talking about the theory.

WP_20130306_001

The next session I attended was SQL query tuning from Klaus Aschenbrenner. This blew me away. I am a bit of a SQL n00b so maybe this is why, but he showed us what is probably simple use of indexes to speed up queries and how the simplest changes can make huge differences to performance. I was impressed by this session and found Klaus to be funny and interesting throughout. So far I had a good start to the week.

WP_20130306_002

The last session of the day about Data Driven windows 8 apps was great. I’ve been trying to learn more about MVVM and WPF. Mark Smith talked about data binding and showed us some live demos again and how easy it was to set this up. I had already some theory in my head so it was great to tie this in with the practical side.

Day 2 covered less interesting topics to me but I still found some very useful things in the sessions I attended but I won’t go into too much detail.

Overall, I found DevWeek 2013 inspiring and motivating. It has made me realise there are a lot more people doing what I’m doing (Most of them better at it!) and this makes me want to learn and gain new skills. Hopefully one day I’ll get the chance to attend again! I recommend it to any developer that is truly interested in what they do 🙂

Windows Phone Emulator No Internet Connectivity Issue Fix

Edit: I was notified that the images were missing last week. This was down to my web hosting changing and I had not backed up the images. I’ve had to take screenshots of the images again so they may not be 100% accurate but at least will give some indication of what was done before.

I’ve started doing a little bit of windows phone development which required me to download the SDK 8.0 from the Microsoft Phone Developer website. This gives you everything you need to get started which is awesome!

So I created a little test app to try and grab me some RSS feed and then I would parse it. To do this I had set up a WebClient and just tried to download the data like this. Ok, build and run in debug mode… BOOM. The program crashed because there was no internet connectivity (ok error handling would normally catch this but it was a quick prototype).

I’ve spent hours looking into the problem and reading loads of forums on what needs to be done to fix it. None of them worked for me until I stumbled upon what I think is my problem (and could be your problem!).

  • Right click on the circled adapter. In this case I’m choosing the wireless card virtual switch as this is my source of connectivity. Open the properties.

Network settings

  • You can see I’ve highlighted the option we want to choose, “Hyper-V Extensible Virtual Switch”. I know it may be unchecked already but once I checked this and chose “Yes” from the next prompt. Things started to work for me.

connection propertiesnetwork dialog

  • Next time I ran the emulator, I managed to get some internet connectivity. I double checked this by opening IE and browsing to a web page.

I just have to say, I have no idea why this happens. I’m not a networking guy so it’s kinda over my head. All I know is that this worked for me. So I’m hoping by posting exactly what I did, it will save someone else the wasted time searching for the answer!

Feel free to let me know if this works or if I am well off the mark with this one 🙂

Google Glass – How transparent are the privacy issues?

I was reading a friend’s article the other day which made me think about all of the privacy issues among other things that may be wrong with google glasses. I recommend you read the full article on his blog. He’s picked up on some important things which will affect everyone, not just the people who buy these glasses!

FULL ARTICLE HERE!

A couple of SQL tips – COALESCE, Replace NULL and Escaping quotes

Today I was working on some queries where I needed to do the following:

  1. Replace a NULL value with 0 or another value
  2. Insert some text into a column which contains an apostrophe (I didn’t know how to escape the single quote)

1. COALESCE / ISNULL
I used COALESCE in the case where I had to have a value for something. This function will take the first non-null value from the list of arguments.

e.g.

SELECT
Manufacturer, 
Model,
Colour,
Price,
COALESCE(Model, Colour, Manufacturer) AS FirstNonNull
FROM
TABLE1 

The use of COALESCE above will take the first non null value listed. So if Model was NULL, it would take the value of Colour. The same happens if Colour is NULL, Manufacturer is taken as the value of “FirstNonNull”. This example isn’t a great real life example but I hope it explains it in simple terms.

If you’re dealing with numbers instead of text, you can also just use COALESCE like this:

COALESCE(Price, 0) AS FirstNonNull

Another way we can replace the NULL value is using ISNULL function built into SQL. If we just want a default when the value is NULL this is ideal. We can use it like this:

SELECT 
Manufacturer, 
Model,
Colour,
ISNULL(Price, 0)
FROM
TABLE1

This will just replace a NULL Price with 0 in this case. Again, not a fantastic example but hopefully shows the idea.

2. Escaping single quotes
I was inserting some text into a table today where I needed to use an apostrophe. Sometimes you will insert text which needs to be grammatically correct so this is an important thing to know.

What I needed to do was escape the single quote in the statement like so:

UPDATE TABLE1 SET Description = 'Have you checked your car's MOT?' WHERE ID = 1;

The compiler would pick up on the single quote or apostrophe in “car’s” and complain as the string would be finished after “car”. So what we need to do is add another carefully placed single quote to tell SQL Server to ignore this apostrophe and insert it into the table.

UPDATE TABLE1 SET Description = 'Have you checked your car''s MOT?' WHERE ID = 1; 

Escaping quotes and other things like this can be useful when you may have to insert formatted text such as HTML into your table.

I’m no expert in this field, but since I have learned these things I want to try and pass them on to others. If you have any questions I’ll do my best to answer them!

Intro to MVVM and WPF by Miguel Castro (Video)

Today I decided to take the plunge and try and learn some MVVM with WPF. I’ve been meaning to do this for a while now but the learning curve put me off a little. Going from windows forms where we write code behind to having a structure like MVVM where we just databind everything!

However, a quick youtube search for “Intro to MVVM” pulled up a video from DevExpress featuring Miguel Castro talking about everything we would need to know to get started with databinding and using commands.

Miguel explains the basics of each part of the framework (View, Model, ViewModel) and gives us multiple ways to achieve the same thing. The best part about this is how he uses simple examples and explains everything at the right pace for someone who doesn’t know anything about WPF or MVVM.

The video is over an hour long and I do recommend you watch the whole thing but if you do just want a basic intro you can probably get away with watching the first 30-40 minutes.

Lastly, this is not my work but I wanted to share this with everyone as the video doesn’t have a great deal of views. Credit goes to DevExpress and Miguel Castro for a fantastic video tutorial 🙂 Thanks!

Source

Using code snippets in Visual Studio with C#

I know snippets have been around for a long time but how many people use them and how many people know about them?

I find them a very useful time saver when writing a new class for example. If I want to write a new property with a GET and SET I can do this by simply writing prop as shown in the image below.

An Example of using prop snippets in visual studio
How to use prop snippet in visual studio

I use propfull which gives me the private variable and then a public GET and SET which will honestly save around a minute compared to typing this out manually. This is what you get with propfull.

An example of propfull snippet
This is the output for propfull

There are a lot of other snippets that can be used, you can find the full list of them here http://msdn.microsoft.com/en-us/library/z41h7fat.aspx

String.Replace – How to use it correctly

I know this is another simple post but it’s little things like this that generally catch me out.

I was trying to use this to replace certain words in a string. I had created a string where I had SQL variables and wanted to replace them with the actual value. Like this:

string name = "Ross";
string id = 1;
string SQL = "UPDATE TABLE1 SET NAME=@NAME WHERE ID=@ID";

I then attempted to use the String.Replace() method to use the actual value of @NAME and @ID.

SQL.Replace("@NAME", name);

This left me scratching my head as nothing happened. After looking up how to use the Replace() method I realised I had to create a new string variable to put the result into or use the same variable again. But the important part is that you cannot just use the Replace() method without somewhere to store the result. You have to do the following.

SQL = SQL.Replace("@NAME", name);

You can even use the Replace() method multiple times like so:

SQL = SQL.Replace("@Name", name).Replace("@ID",id);

I hope this has been helpful! Again, very simple but if I needed to look up how to do this I figure others will as well 🙂