Perpetually In Beta.

Archive for the ‘Steve Yegge’ tag

Til Death Due Us Part – Project Euler – Problem 3

without comments

Project Euler "is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems."

Lot's of programmers recently have been using Project Euler as a form of 'Code Kata:'  a philosophy that believes that greatness comes through diligent practicing.  Steve Yegge written on the subject as well as Kathy Sierra.  The idea is that you can be insanely great at anything if you practice at it.  Sure, some people are natural geniuses — and you will never be better then them.  But if you want to, you can be in the top 1% of learned people on any particular subject.

I'm not sure if I believe them, but heck, i think that the idea inspires lots of mediocre programmers ( like myself ) to wake up each day and learn new stuff.  And that in itself is worth something, right?  I do believe that if you just set aside one hour per day to learn something new, you can become quite proficient in anything at all in a shorter period of time then you even imagined.  Most people have a hard time with this though.  They have a seeing that just one hour per day can lead to incredible results.  I myself was in this category until I learned the 'Til Death Due Us Part' productivity method.  On my Google homepage, I have the following widget on display:

The 'Countdown' widget shows me the (estimated) number of days I have until I die.  Instead of brimming over with horrible sadness everyday, the huge number of days I have left actually motivates me.  I just think about this simple fomula:

Number of Hours Per Day * Number of Days Til Death = Total Number of Hours

If I started today learning Ruby and practiced Ruby one hour per day, by the end of my life I will have practiced Ruby 17,193 hours.  What are you NOT going to be good at after practicing for that long?  With that in mind, you have no excuses.  It is never too late for you to learn anything… well, provided you are not already 83 years old.  Then I'd stick to shuffle board.

I've been teaching myself Ruby the last couple months and just recently started going through the problems on Project Euler.  Problem 3 posed the following question:

The prime factors of 13195 are 5,7,13, and 29
What is the largest prime factor of the number 600851475143?

From wikipedia, "In number theory, the prime factors of a positive integer are the prime numbers that divide into that integer exactly, without leaving a remainder."

My solution to the problem in Ruby is below:

The first dilemma I faced while solving the problem was generating a list of prime numbers.  I'm going to be honest, I didn't generate squat.  I kind of cheated.  I found a list of 10,000 prime number online.  When the class PrimeFactorFinder is initialized, the list of numbers is opened, the prime numbers are "scanned" in using Regular Expressions and added to the array @PrimeNumbers.  There is an way to actually generate prime numbers in Ruby I discovered.  One can use the mathn standard ruby library as seen below:

require 'mathn'
primes = Prime.new
factor = primes.next

The next hurdle was finding the list of prime factors.  This was found by modulo(ing) the numberToFactor by each prime number. If the former expression yields '0′ then the prime number divides into the numberToFactor exactly and is a 'Prime Factor.'

All in all a pretty simple exercise, yet it required me to use Ruby's Arrays, File IO, and Regular Expressions. Code Kata indeed.

Written by codingwithoutcomments

August 28th, 2008 at 1:20 pm

Best Blogging Options for Programmers / Source Code Formatting

with 9 comments

Hello! (in french)

Hello! (in french)

I think everyone that starts blogging inevitiably writes things like, "This is my first blog, um, i don't know what to say, um, oh this is awkward isn't it.  Hello world!"  But, unlike everyone, I do actually have something (somewhat interesting) to say.  Which is strange, but nice I suppose.

The idea for this post started when I was reading a post on Stackoverflow, the new joint venture between Joel Spolsky and Jeff Atwood, entitled 'How you can tell you are ready to start your own blog?'

The post went as follows:

Hi, I'm curious whether I should start my own blog or not.

I've been working as a programmer for 3 years and have some good and bad experience I can share. On the other hand I'm not sure I will be able to post very often (maybe even less then one time per week).

They say writing a blog will improve your writing skill and it seems appealing to me. I've to admit sometimes I've a problem to write down my ideas in a short clear way so people can understand them easily. But I don't want to write for the sake of writing. I think that blog helps me only if there will be some readers and therefore some comments. In order to have readers I've to put some interesting stuff there pretty regularly, haven't I?

So how can you tell when it's time for one to start blogging?

The replies are now legendary.  Besides improving your writing skills, the reasons to start a blog as a programmer are quite numerous.

Why You Should Blog As a Programmer

The mere act of writing your thoughts and actions down is a form of teaching.  Blogging cements the knowledge you have acquired in your brain 8.9 times better then just mere-programming or even verbally explaining something to someone.

Steve Yegge writes in 'You Should Write Blogs',

"…that blogging is a source of both innovation and clarity. I have many of my best ideas and insights while blogging. Struggling to express things that you're thinking or feeling helps you understand them better."

Jeff Atwood talks about in his podcast how he doesn't feel like he has actually done any work at all if he hasn't documented it and put it in the public sphere.  Being so exposed, he knows he is accountable to the thousands of people who read his blog.  This, in turn, causes him to produce better quality work (he hopes).

When blogging as a programmer, you are creating a documented public portfolio of what you are working on, a resume that is more then a few lackluster pages in Word with various haphazard skills and, in my case, a not so long job history as well as a reference for your future projects.

But better then that, simply the act of blogging can lead to success and fame.  In how to achieve blog success in one easy step, Jeff Atwood writes about his success from blogging,

I won't say I got my job here at Vertigo back in 2005 because of this blog, but it was definitely a factor. I was interviewed on .NET rocks, and I've been interviewed online not once but twice. I've been invited to speak at conferences. I am approached for book deals every few months. I exchange email regularly with Steve McConnell, one of my programming idols as a young adult, and he once asked me for advice on blogging. Joel Spolsky actually recognized me and invited conversation when I attended the Emeryville leg of his world tour. Charles Petzold sent me, completely unprompted, a signed copy of his latest book. People offer to send me incredibly cool free swag on a regular basis.

Jeff goes on to say that he thinks that blogging is "the most important thing I've ever done in my entire career."  Wow.

So, inspired to the teeth by reading all of the above, I set out to start my own blog.  I went to google, clicked "create blog" at Blogger, clicked "Write New Entry," and started working on a piece about, i don't know,  the Template Pattern in Ruby or something.  I pasted some code into my blog, moved my mouse up to the formatting options and stopped dead in my tracks.

There was no Code Formatting option!

Shame shame know your name Google.  How can you write a programming blog without code formatting options?  (Answer: Black Bear).  I realized this was a question for the hive mind so I asked the good folks at Stackoverflow what is the best blogging host for programmers/code formatting?  The results were wide and varied.

Blogging Options

Most people seemed to agree that Wordpress was the best overall blogging platform in terms of options and flexibility.  These same people stated that one is better off self-hosting wordpress or using a hosting provider and getting a domain name rather then using the hosted wordpress (Wordpress.org vs. Wordpress.com).  Self-hosting has a couple of main advantages as stated by Christain:

  • You will be the master of your domain name: when you want to change hosting company or software, you will be able to do so. You don't want to change your domain name when you have thousands of readers and a Google PageRank of 5.
  • You will be able to customize your software (with little effort) to add the many plugins available.

I decided to go with Wordpress and bought a one year webhosting package from DreamHost (recommended by wordpress) for a little under $100 for like a bagillion gigs of space and bandwith.  Plus, setting up wordpress on Dreamhost was insanely easy.  They have a one-click installer.  It took 2 minutes.  They also have a one-click installer for Subversion which i found cool.  You are now viewing the results.

Code Formatting Options

1.  If using wordpress, the WP-Syntax plugin provides clean syntax highlighting using GeSHi — supporting a wide range of popular languages. It supports highlighting with or without line numbers and maintains formatting while copying snippets of code from the browser.

Below is an example of GeSHi formatted code in Ruby using the wordpress plugin,

class Report
  def initialize
    @title = 'Monthly Report'
    @text = ['Things are going', 'really, really well.']
  end
 
  def output_body
    @text.each do |line|
      output_line(line)
    end
  end

Geshi isn't limited to wordpress, but can be used to "highlight code on ASP pages, numerous blogs, wikis and forums."

2.  Use Windows Live Writer with the Insert Code plugin. The problem with this option is that you are limited to blogging on a Windows (sic) Machine, and it only formats the the following languages:  C#, HTML, MSH, JavaScript, Visual Basic and TSQL.

3.  SyntaxHighlighter is a Javascript based Code Formatting mechanism that can easily integrate into most blogging systems.  It supports the following languages: C++, C#, CSS, Delphi, Java, JavaScript, PHP, Python, Ruby, Sql, VB, XML/HTML.  Ellis Web posted a great article on Using Syntax Highlighter to Format Code in Wordpress.

4.  If using the blogging engine Chyrp, Geshify or Feather can be used for Code Formatting.

5.  Mango Blog and BlogCFC both support code blocks out of the box.

6.  CodeHTMLer is a simple program that translates plain text code into a colorized HTML version of the code.

7.  If you use TextMate in Mac OSX, BitButter has a great post on how to display your TextMate code (formatting included) in WordPress.