Solving Algorithms – Bubble Sort

Why are solving algorithms Adnilson, shouldn’t you work on building applications? Yes, you are right! The thing with algorithms is that it helps you solve problems and are good to make your mind look for solutions. This way you build a blueprint and whenever you get a problem you tackle the problem instead of being tackled by it.

In Ruby things sometimes are too easy and today I will go back to school and review the bubble sort, this algorithm is not the most efficient way of sorting but is a simple and cool exercise.
In Ruby if you want to sort say, an array you can do it by doing array.sort and bam! There it is.
The code:


length = array.length - 1
loop do
  swapped = false
  (0...length).each do |i|
    if array[i] > array[i+1]
      array[i], array[i+1] = array[i+1], array[i]
      swapped = true
  break if !swapped

puts array

I guess the code speaks for itself. In a lower level language the program would have an extra variable to store the value of one of the array contents in order to swap.
The first time I did this was in Pascal, then Fortran in college.

Solving Algorithms – Title Capitalisation

Today I encountered an algorithm to solve and the amazing Ruby syntax made me solve this problem with a succinct code with testing included.
All words in the title should be capitalised except a few ones like “a”, “the”, “to”, “at”, “in”, “with”, “and”, “but” and “or”.

To make this very short the solution is here:

require 'test/unit'

class MyTest < Test::Unit::TestCase
  test "capitalization" do
    assert_equal title_case("i love solving problems and it is fun!"),
                           ("I Love Solving Problems and It Is Fun!")
    assert_equal title_case("wHy DoeS A biRd Fly?"),
                           ("Why Does a Bird Fly?")

def title_case(title)
  lower_list = %w(a the to at in with and but or)
  words = title.downcase.split
  words.each do |word|
    word.capitalize! unless lower_list.include?(word)
  title = words.join(' ')

Ruby permutation method rocks!

Early today I was doing my first homework assignment for the SaaS course by University of California – Berkeley, and was making a Rspec test work. This method was meant to take 2 arguments, one array with integers and one integer. What we have to do is sum two numbers in the array until it gets equal to the second argument. Since I come from a Pascal and C background my mind started to think low level, and then I had to say “calm down Adnilson, this is Object Oriented” but, that didn’t keep me from trying. After hitting a wall I went to the Array documentation where I found Ruby’s permutation  method that permitted the program to sum all numbers in the array until it matches n, otherwise returns false. I don’t know if other programming languages have the same method, but this made me feel so happy! Below I will show the code:

def sum_to_n? arr, n
if arr.empty? || arr.length == 1
  elsif arr.permutation(2).any? { |a, b| a + b == n }

How to Print Urls with Nokogiri

It has been a while since I made a post here, the excuses will be off and I’ll just get to the post.

I will show a way of printing a url by building a Ruby script with Nokogiri.
What I want is to print all the compressed files in the website, these are contained inside the td (table data) of tr (table rows).

require 'nokogiri'
require 'open-uri'

class Scraper
  def initilize(url)
    @url = url

  def file_url
    page = Nokogiri::HTML(open(@url))
    rows = page.css('table tr')

    rows[1..-2].each do |row|
      hrefs = row.css("td a").map { |a|
        a['href'] if a['href'].match(".tar.gz")

      hrefs.each do |href|
        puts @url + href

scrap ='')


My first programming interview

First of all I finished after work the Ruby Koans, it was an awesome experience and made me understand much more about testing. It happened so fast that I felt sad for being so short but at the same time felt an inner feeling that I have accomplished something good. Rails Tutorial is next (for a second time).

My first interview. Oh when I received a phone call from a recruiter scheduling an interview for a Python position I was in complete awe! I remembered her saying: “next Tuesday at 4:30PM”. I was like: “Yes! Sure! “The following week I studied Python as a voracious bear that hunts his prey, mastered my skills and was prepared. When I went to the subway and read the confirmation email, saw that it was changed to 4PM, and the time was 3:55PM. I rushed to call the recruiter but she didn’t answer at first, the while on the train called again and got in touch with her.
I arrived late and had to wait an excruciating 20 minutes! Finally comes the girl that was going to interview me. She asked me a few pseudo technical questions which I got right and then asked me if I had completed my college education which I said no, and then she said that I wasn’t suitable for the job because of that. No tests whatsoever, then the entire interview questions were about Ruby and soon I realized that she had 0 knowledge about programming. She didn’t know that Ruby was an Object Oriented Language and that Ruby is not Ruby on Rails the later is a framework and the former is the language.
I was really pissed off and lost an opportunity (a story for a later post). It can be seen that the interviewer haven’t read my resume and also does not have respect for other people’s time.

After all life goes on and I grew from this experience and attacked coding with more power. There will be other interviews and the job that I want will come true. With hard work, habit and will-power, great things happen.

Ruby Koans Dice Project

I finish my day with this project in the amazing Ruby Koans learning method, and had completed 216/282! Got to see the Ruby documentation and feels like I am getting more into the syntax.
Here is the code I made in the about_dice_project.rb:

# Implement a DiceSet Class here:
class DiceSet
  attr_reader :values
  def roll(num)
    @values = []
    (1..num).each { |count| @values << rand(1..6) }