Rails

Rails for Zombies

Rails for ZombiesOn Thursday of this past week, Chris and I did a brown bag “lunch and learn” at work. Our original plans fell through, so we decided to hit up Rails for Zombies and see what it is about.

I really like the format of the tutorial and the way that the labs are constructed. If you follow the process exactly, you create an account so that they can track your progress so you can return at any time and pick up right where you left off. Then you watch several 5-10 minute videos and then complete an interactive session in your browser after each one.

Here is a screenshot of the first video’s title screen. It definitely helps you get a feel for how *in* to this the good folks at Envy Labs are.

Lab One Video

The goal of the project is that we are building a “Twitter for Zombies” so that they can be all social and stuff. All of the code that is written is around that goal. Here’s what it looks like when you get into the lab.

Lab One Exercise

You can see that after I write my code and submit it, I get instant actual IRB-like feedback. If I were to have made a syntax error, I’d get that back, as well. It seems to be doing true evaluation, as Chris entered some alternative, more advanced ways of performing the labs and it was all evaluated correctly, too.

Lab One Exercise Solved

There is definitely a benefit to not having to set up Ruby and Rails on your machine before you get some exposure to Rails. Just like if I was trying to pick this up by pairing with someone, you don’t start at the beginning with this project. There is no project creation, scaffold generation, database setup, etc. You start in on a project that is kind of already underway.

Several things you do are never really explained and some things you do are explained at the very end (like generating links in the view based on named routes). I supposed I could have suspended belief a little until I got to lab five to learn it, but I ended up asking Chris a lot of questions as we went along.

As I said, I really like this format for teaching and conveying new programming topics to people. I’m really impressed at the work that Envy Labs put in. At the same time, you could complete this entire lab and not know how to even begin your own Rails project. However, if you couple this training with all of the existing Rails tutorials that try to get you up and running in 10 minutes and I think that you could have the knowledge you need to get pretty far into making your own Rails app from your own concepts.

Git

Git Immersion

Got GitI’ve begun tackling my 2011 technology resolutions. It was as if the good folks at EdgeCase read my blog and deigned to help a brotha out. They’ve recently launched Git Immersion, which does for Git what their Koans did for the masses. That is, it offers a simple, step-by-step way for someone to learn a technology.

I already learned something. Doing Lab 3, it says to issue this command

git commit -m 'First Commit'

When I do, I get this error

error: pathspec 'Commit'' did not match any file(s) known to git.

A little of my Google-Fu and I find that my windows command prompt is anti me using the ‘ here. I change the command to

git commit -m "First Commit"

and I get the right result

[master (root-commit) d23d927] First Commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hello.rb

Another thing I learned. When I typed “git commit” with no -m (Lab 8), it brought up VIM. Guess what? I’m such a n00b that I couldn’t figure out how to save my freaking file. Thank goodness for StackOverflow here to tell me to hit Esc then type :wq and then the enter key.

That’s it of my revelations so far. As I’m writing this, I am through the first 10 labs and will do some more after I post this. I’m really looking forward to learning Git because I like the way that it encourages you to work. On to some more learning…

Goals

2011 Technology Resolutions

ResolutionsI try to learn at least one new thing every year in the technology field. In 2010, I learned (and build a production application from) Asp.Net MVC 2.0. I also learned Windows Phone 7 development and even got an app in the Marketplace called Nerd News. You can see a little more about it here or by searching for Nerd News in the Marketplace.

In 2011, I’m beginning the year with the goal of learning more about Node.js, Socket.IO, and Git. I had hoped that CodeMash might have some Node stuff this year, but it doesn’t. Maybe I can find or start an open space about it…

Do any of you try to learn a new technology every year? If so, what are yours for 2011?

Project Euler

Project Euler Problem 4

TACOCAT is a palindrome!Continuing on from my previous adventures in Euler, we now come to problem 4. Problem 4 asks you to find the largest palindrome that is the product of two 3 digit numbers.

My solution is brute force, and originally, I started two loops both at 999 and counted down, figuring the first palindrome found would be the largest. I was wrong, however, and was required (sticking to brute force) to check all products for palindromes and just keep the largest. The “count backwards” code returned “Using 995 and 583, the max palindrome is 580085”, which is incorrect. My correct code is as follows:

using System;

namespace ProjectEuler
{
	/// <summary>
	/// A palindromic number reads the same both ways. The largest
	/// palindrome made from the product of two 2-digit numbers is 
	///  9009 = 91 x 99.
	/// Find the largest palindrome made from the product of two 3-digit numbers.
	///  http://projecteuler.net/index.php?section=problems&id=43
	///
	/// The answer is 906609
	/// </summary>
	public class Problem4
	{
		public static void Main(string[] args)
		{
			var max = 0;
			var theI = 0;
			var theK = 0;
			
			for (var i = 100; i <= 999; i++)
			{
				for (var k=100; k <= 999; k++)
				{
					var product = i * k;
					if (IsPalindrome(product) && product > max)
					{
						theI = i;
						theK = k;
						max = product;
					}
				}
			}

			Console.WriteLine("Using {0} and {1}, the max palindrome is {2}", theI, theK, max);
		}

		public static bool IsPalindrome(int number)
		{
			var forward = number.ToString();
			var reverse = forward.ToCharArray();
			Array.Reverse(reverse);

			return forward == new string(reverse);
		}
	}
}

That returns “Using 913 and 993, the max palindrome is 906609”.

After you put in the correct answer on the Project Euler site, you are allowed to then view the forums to discuss your answers. I found some interesting math inside that made other algorithms much more efficient. Here is what I gleaned:

A six digit palindrome would be in the format abccba. If we take our real answer of 906609, that could also be written as 9(100000) + 0(10000) + 6(1000) + 6(100) + 0(10) + 9(1). The same way, our generic answer can be written as 100000a+10000b+1000c+100c+10b+1a. Simplified again, that is 100001a+10010b+1100c. You can factor 11 out of that, leaving you with 11(9091a + 910b + 100c). That means that the product would have to be easily divisible by 11 (saving you the lengthy – by comparison – palindrome check). My original algorithm ran in 9.219022 seconds on my Mac Mini running Mono. When I add in the “divide by 11 check” to short-circuit every palindrome comparison, the algorithm now runs in 1.63679 seconds. That is a HECK of an improvement. Math… I think this might just catch on!

Project Euler

Project Euler Problem Three

Optimus PrimeIt has been almost two years since I tackled Project Euler Problems One and Two. I really wanted to get back into it, so there really is no time like the present to do the work. I’m on my Mac Mini right now, so I developed this in C#.Net using MonoDevelop.

This problem needs us to find the largest prime factor of a very large number. Obviously, to do this, you need to be able to generate a set of prime numbers to work with. I have an IsPrime method that uses a very brute force method (with the shortcut of only checking as high as the square root of the number). I Googled around for ways to generate primes and looked into the Sieve of Eratosthenes, but it was a little complicated for the time I had allotted myself to work on this problem (basically the ten minutes until I had to put my son to bed). Plus, it turns out that this entire piece of code runs in under a second on my several year old Mac Mini, so there was no need to optimize yet. I know that later Project Euler problems also deal in primes, so I may need to break it out then.

Once I had that method in place, it was really a simple matter of working up to the square root of the number in the problem, 600851475143, using the same shortcut. If the number in my loop was prime and evenly divided into our number, I stored it as the current largest number. When I was done, whatever number was currently in that variable was our champion. Pretty simple logic.


using System;

namespace ProjectEuler
{
	/// <summary>
	/// The prime factors of 13195 are 5, 7, 13 and 29.
	///  What is the largest prime factor of the number 600851475143 ?
	/// http://projecteuler.net/index.php?section=problems&id=3
	///
	/// The answer is 6857
	/// </summary>
	public class Problem3
	{
		protected static double number = 600851475143;
		
		public static void Main(string[] args)
		{
			double limit = Math.Floor(Math.Sqrt(number));
			double currentLargestPrime = 0;

			for (double i = 2; i <= limit; i++)
			{
				if (IsPrime(i) && (number % i == 0))
				{
					currentLargestPrime = i;
				}
			}			
			
			Console.WriteLine(currentLargestPrime);
		}

		public static bool IsPrime(double n)
		{
			if (n%2 == 0) return false;

			var upperLimit = Math.Floor(Math.Sqrt(n));

			for (double i = 3; i <= upperLimit; i++)
			{
				if (n % i == 0) return false;
			}

			return true;
		}
	}
}

Have you attempted this problem yet? What is your solution? I’d love to see them. You can post it in the comments or post a link if you’ve blogged it already.