Friday, February 22, 2013

Partition and Splat in Quick Sort

The Ruby Array has a partition method that takes a block and returns an array of elements for which the block evaluates to true, and a second array of elements for which the block evaluates to false.

This method can be used in a quick sort, which involves selecting one element to be a pivot and putting all other elements into either a "less than" array or "greater than" array and sorting each other those the same way.

This is a recursive sort, which means it calls itself.

def quick_sort number_array
  return number_array unless number_array.size > 1
  
  pivot, *the_rest = number_array
  
  less, more = the_rest.partition { |number| number < pivot }
  
  quick_sort(less) + [pivot] + quick_sort(more)
end

this_array = [22,66,4,44,5,7,6,8,77,33,8,99,6]


puts "unsorted: #{this_array.inspect}"

puts "sorted: #{quick_sort(this_array).inspect}"

Also interesting is the splat operator (the asterisk), which enables the new variable to take more than one parameter.  When an array is assigned to multiple variables, the splat operator allows an item to take enough elements to make the assignment work.  Above it is assigning one element to be the pivot and the rest to be partitioned.

When defining a method, the splat operator can be used with one of the parameters, enabling it to accept multiple parameters as an array.  Although it can be anywhere in the parameter list, using splat on the final parameter is preferred because extra parameters will all go into the array instead of changing how other parameters are assigned.

As used above, elements are used arbitrarily so the order doesn't matter as long as the_rest gets all but the element assigned to pivot.  I guess you could think of the left side of the assignment as the method definition's parameter list and the right as a call to that method.

Friday, September 14, 2012

Fixed vs. Growth Mindset

You are, indeed, stretchable....


Sunday, July 17, 2011

Learning Git

Scott Chacon: Getting GitImage by fraserspeirs via FlickrTo learn a challenging topic, first find someone who knows both the topic and how to teach.  For git, Scott Chacon is one of those guys.  Scott is a Rails developer working for github, THE place to host your open source git repository.  Here's Scott:

I recommend trying everything discussed yourself as the video progresses, stopping when you need more time or need to google what you don't understand.
Enhanced by Zemanta

Friday, July 8, 2011

Become a Learning Machine

BinaryImage by Xerones via FlickrIf you find your education lacking, its now easier than ever to learn your way out of it. Universities all over the world put their lectures online. You can find one that teaches the topic of interest in a way that works for you.

And if you don't understand something, you can STOP the lecture and google it until you do. Replay the few seconds of video to make sure you heard it right. Do whatever it takes. You're in control.

Become a learning machine. Then no one can stop you.

Case in point. I lack a computer science background and get stumped when asked about basic software engineering concepts like sorting algorithms.  And I'm a software engineering. Or at least I play one on TV. :-)

So I finally decided to do something about it. I've found several courses on the topic. Here's a great one from Australia:

http://www.youtube.com/watch?v=RpRRUQFbePU

All the world's knowledge is being made accessible to you. Seize the opportunity!
Enhanced by Zemanta

JavaScript Semicolon Insertion

Code reviewImage by richardmasoner via FlickrHere's a good discussion of when a Javascript developer should use semicolons.

Basically, semicolons are statement separators and there are situations where a line break won't be interpreted as the end of a statement.

Many Javascript developers put them at the end of every line, but this is unnecessary.  Its better to actually understand the cases where a line break won't end a statement.

If you use a minification tool that doesn't interpret line breaks correctly, bugs could be introduced.  But if you use semicolons only where you need to, a bad tool will probably break most things right away. And that will be obvious.  Use semicolons ALMOST everywhere and then you won't discover the problems until you test specific lines that don't have them.
Enhanced by Zemanta

Saturday, April 17, 2010

Color Tools That Will Make Your Web Shine

Creative Arts Matthew Boulton CollegeImage by jisc_infonet via Flickr

Haven't checked all of them out yet, but it looks like a very useful list of color tools.

I'm creating a new resume tool and looking for something that will let me find a professional color scheme.
Reblog this post [with Zemanta]

Wednesday, February 24, 2010

Intuit Ruby with Ruby Koans

SAN FRANCISCO - SEPTEMBER 25:  A zen garden cr...Image by Getty Images via Daylife

The ruby koans plugin promises to "walk you along the path to enlightenment" in your quest to understand the finer points of ruby.

A koan is a Zen Buddhist concept meaning "a story, dialogue, question, or statement; the meaning of which cannot be understood by rational thinking, yet it may be accessible by intuition."

Koans encourage test driven development by presenting you with tests of ruby code and challenges you to make the tests run successfully by fixing the code. Presumably, once you have acquired the intuition targeted, your test will run.

Ruby can be challenging but has a lot of power and flexibility. And some day, maybe even scalability :-)