Continuous Integration with GitHub and AppHarbor

There really is no excuse anymore to not set up some sort of automated deployment. It used to be that you had to spend all day installing software, configuring files, and wrestling with error messages to get to the point where you can push a button to automatically commit code to a repository, build the code, run the test and deploy it to a server. Even when it took all day to setup it was worth it for the time you save every day during development.

Now it only takes a few minutes.

If you don’t already have a github account, head over there and setup and account. Create your first repository and make your first commit. Create an AppHarbor account, and click on Your Applications to create a new application. Now you have your repository and deployment server setup and ready to go.

The next step is to go back to your repository in GitHub, click on Admin, then Select Service Hooks on the left. Choose the AppHarbor Service hook and follow the instructions. You’ll need to go back to AppHarbor, open your application and click on “Build URL” to put your applications URL into the clipboard. It will look something like this…

https://appharbor.com:443/applications/
{applicaiton_slug}/builds?authorization={token}

You’ll need the application slug and token to setup the service hook on github.

If your repository is private you need to add the “apphb” user as a collaborator on your repository to allow AppHarbor to fetch your code.

That’s it! The next time you push your code to github, AppHarbor will be notified, fetch the code, build it, run your tests and deploy it to your unique URL. I use GitHub for Windows, so that I really only have to push one button when I’m ready to deploy.

AppHarbor will automatically detect and run all unit tests in your solution, as long as they were created using one of the following frameworks (MbUnit (2.4.0.0 – 2.4.2.175), MSTest, NUnit (2.4.8.0, 2.5.0.0 – 2.5.3.9345 and 2.5.4.0 – 2.5.9.10348), xUnit.net (1.1.0 – 1.9.0), MSpec).

I freakin’ love this!


5 Real World Recursion Examples

I recently conducted an interview in which I asked the young woman who was just out of school to explain recursion. She answered correctly and explained it in the most obvious way (and what we are taught in school) using the example of Fibonacci numbers. This got me thinking about recursion and that I should have asked her to give me a real world example of when you would use recursion. Then I had trouble thinking of an example of when I had used recursion.

It’s true we don’t use recursion in everyday programming much, and are generally discouraged from doing so unless it really makes sense. You can have stack overflow issues if you are working with large trees, or are unsure of how deep the function calls will go. But there are some common and some uncommon but interesting things happening with recursion in the real world. Here are five that I could come up with. The code is in C#.

1. Sorting

While Most languages have a built in “sort” function that you can call these days, and it’s not necessary to write your own, sometimes it’s good to know how they work. You may even be asked to write a sort algorithm in an interview. Several of the popular algorithms for sorting use recursion.
Quicksort is an example of a sorting algorithm that splits a contiguous memory location in two, divided by an arbitrarily selected value called a pivot. All values less than the pivot go into the left partition, and all values more than the pivot go into the right partition. Then the sort recursively drills down into the partitions in the same way until the entire list is sorted.

The main recursive function for a quicksort looks something like this…

       static void Quicksort(char[] array, int left, int right)
       {
            int pivot = Partition(array, left, right);

            if (left < index - 1)
                Quicksort(array, left, pivot - 1);

            if (index < right)
                Quicksort(array, index, right);
        }

2. Tree Traversal/Searching

Pretty much anything that can be represented in a tree structure can be traversed (or built) using recursive functions. Let say we have a basic Tree class that defines a tree of integers…

class Tree<int>     {         public Tree<int> Left { get; set; }         public Tree<int> Right { get; set; }         public int Value { get; set; }     }

Left represents the left sub tree and Right representing the right sub tree. We could traverse the tree by recursively dipping further down into the tree’s left and right branches like this.

        static IEnumerable EnumerateTree(Tree tree)
        {
            if (tree == null) break;
            foreach (int i in EnumerateTree(tree.Left)) 
                return i;
            return tree.Value;
            foreach (int i in EnumerateTree(tree.Right))
                return i;
        }

3. Parsing Context-Free Grammars in Compilers

I’m not going to try to explain Context-Free Grammars here, you can Google it if you want more information, but basically it is a set of grammar rules, used to define a programming language. If you have ever written a compiler or taken a compiler class in school, you know that a compiler must parse every program to check for syntax errors and they sometimes use recursive functions to do so. There are non-recursive parsers, but we won’t get into that here.

Context-Free Grammars lend themselves well to recursion because they can be structured into Parse Trees. Parse Trees represent all of the syntactical paths a language can legally take for a given construct. Here is a generic parse tree for an assignment statement.

A recursive algorithm can be used to traverse the parse tree similar to the Tree Traversal algorithm in number 2.

4. Fractals and Cancer Prognosis

Fractals are all around us. They are naturally recursive patterns that are found everywhere in nature. Fractals are infinitely repeating patterns such as in a tree or a snowflake. Look closer and you still see the same pattern, no matter how close you look. Fractals can be built recursively.

You could write a computer program using recursion to build a drawing of a tree. But there are more practical uses of fractals in programming. Fractals occur in human biology. Fractals, and thus recursion, are being used to analyze tumors found in patients with various types of cancer. This article explains better than I can how cancer cells are believed to be more fractal in nature than normal human cells. More and more computers are being used to aid in cancer prognosis.

5. Cheating at “Words with Friends”

I never condone cheating, but you could potentially write an algorithm (using recursion) to find you all of the anagrams of the letters you possess, then match them up with real words using a dictionary web service to see which ones are actual words. The anagram algorithm might something like this, in which the starting prefix is “” and the word is all of the letters that you have.

        static void OutputAnagram(string prefix, string word)
        {
            if (word.Length <= 1)
            {
                Console.WriteLine(prefix + word);
            }
            else
            {
                for (int i = 0; i < word.Length; i++)
                {
                    string cur = word.Substring(i, 1);
                    string before = word.Substring(0, i);
                    string after = word.Substring(i + 1);
                    OutputAnagram(prefix + cur, before + after);
                }
            }
        }

This algorithm works in finding all combinations of the letters using ALL of the letters, but there are obviously a lot more. For example, for the letters of scta,this algorithm finds the combinations (scta, scat, stca, stac, sact, satc, csta, csat, ctsa, ctas, cast, cats, tsca, tsac, tcsa, tcas, tasc, tacs, asct, astc, acst, acts, atsc, atcs). But what about cat, sat, at? I’ll leave the rest of the code up to you.

In what ways do you use recursion?


Pigtails and Programming Do Mix

When I was eight I opened my brothers programming magazine, turned to the back and found something glorious. A list of code that when typed into a computer would make it do something cool. I started to type one of these BASIC programs into our Apple IIe in the back room of our house. I could barely reach the keyboard. After typing for an hour with my two pointer fingers, I ran it. It worked. I started messing around with the code, mostly causing error messages, but eventually getting it to do what I wanted to. It was magic.

A year later I entered a programming competition at my school. The only people entered were myself and the most nerdy kid in school, we’ll call him Seth. I wanted badly to beat Seth, because not only was he the most nerdy, he thought he was the smartest.

I took out a number two pencil and a hundred pieces of lined paper and began to write a program. I called it “Detective Agency”. It was a game in which the user was working as a detective in a major city and was given various cases to work on. It was all multiple choice. I think you could actually choose which case you wanted to work on.

A. Murder
B. Robbery 
C. Vandalism

I know, cute. I wrote the entire program on that lined paper. There were upwards of 2,000 lines of code. Most of it looked like this…

IF (PROMPT() == 'A') THEN
GOTO LINE 2456
ELSE IF (PROMPT() == 'B') THEN
GOTO LINE 456
ELSE IF (PROMPT() == 'C') THEN
GOTO LINE 3
ENDIF

For all of the Seth’s out there that are saying “That’s not correct syntax for BASIC 1.9.8.5″, bite me. I don’t keep useless shit in my brain.

After what seemed like a month (but was probably actually a weekend) of typing and fixing and testing and wrestling, I had a finished working program. I entered it into the competition and won. Take that, Seth. I was automatically submitted into the “regional” competition, where I showed up with my mom in my pigtails and was the only competitor. I won that too.

Because my program was the shit, I went on to the state competition and presented it in front of a panel of judges and my other older, more experienced, competitors. I was the youngest person in the room and I was terrified. The other competitors programs  had cool things like “graphics” that I had never seen before. They were speaking a foreign language and using acronyms for things I had never heard of. I didn’t even know what an acronym was. But I presented my game and they clapped and looked at me like, “Aw, you’re cute.” I thought for sure it was a pity clap and that I would come in last.

I got second place. Second only to the 15-year-olds with the graphics program that drew the Statue of Liberty on the screen.

My point is, I sucked as a programmer at first. My first program was scrambled mess of spaghetti code written with a pencil on lined notebook paper. But it worked and it was my program and it was beautiful and pity or not, I won a competition. If you have an idea for a program in your head, just write it. Don’t worry about how inexperienced you are. You’ll learn how to use the tools and the languages. You’ll learn how to write good code, you just have to start.

If you have a passion for it, it will be awesome. Maybe no one will ever use it, maybe you’ll even get criticized for it, and you WILL get a million more experienced programmers telling you what you did wrong and how stupid you are. Programmers are just assholes like that. But what you will gain from taking an idea that was in your head and building it from scratch, will be invaluable. And fuck them. You wrote a program.


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: