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 }

Taking more than you can handle

Some time ago a friend gave me a coding challenge which I decided to make it in Ruby on Rails, I hesitated because was expecting that an Enterprise would give me a task to build something in JavaScript/HTML/CSS, so the time passed. Then I applied for a company that is hiring any kind of developer that understands how the Full Stack works and got another coding challenge. This time in Nodejs and Express. Perhaps I do not need to say that I have to learn a whole new technology to get the challenge working and net the job. Besides all of this I enrolled in a course from Berkeley University of California called SaaS (Software as a Service) and the framework they use is… You got it! Ruby on Rails!
How am I going to do all of this? Since I like to finish what I’ve started (which sometimes is not possible), I feel stressed about this situation and wasn’t expecting all that’s happening. I heard somewhere that it is not about the how but the why. Why do I want to be a Developer? Why do I want to learn? Why am I pushing through all the resistance? Why do I care about Self-Development and read/listen to a lot of books? The answer is simple, I want to become the best, learn with the best, be on the top 3%. Is it simple or easy? I don’t think so. Do I think it is possible? Absolutely.

var and let the difference in JavaScript

Javascrit has something called Hoisting. This is when all variables and functions are loaded before the program runs.

function loadProfiles(userNames){
// some code...

    var loadingMessage = "Hold a sec... Profiles loading!";
// some code...

var loadingMessage = "Loading your profile!";

What will happen here is that the first declaration of loadingMessage will be loaded first,then the second declaration will be loaded and overwrite the first. Which will not give us what we want.
To solve this issue ES6 or ES2015 came with a solution: let !

function loadProfiles(userNames){
// some code...

    let loadingMessage = "Hold a sec... Profiles loading!";
//some code...


let loadingMessage = "Loading your profile!";

These variables will load locally, saving memory without overwriting each other. If you console.log the loadingMessage variable after the loadProfiles method, the result will be different from if you ran inside the loadProfiles scope.
What is a scope? I’ll tell you later.

Hexadecimal and Colors

This week will be more related to the Front End side of web development. So I thought about colors which are used everywhere. The web is one of the technologies that uses all the human senses, it evolved to become an everyday tool and entertainment.
Today I will teach you how to calculate hexadecimal numbers and how they can be related to numbers.

Hexa-decimal: Hexa comes from the Greek word héx which means six. The decimal I’m sure that you already now.
Decimal numbers run from 0-9.

81 (decimal):
multiply the left by 10, (8x10)
multiply the right by 1, (1x1)
We get 81 decimal.
81 (hexadecimal):
multiply the left by 16, (8x16)
multiply the right by 16, (1x1)
By adding them we get 129 hexadecimal!

When we work with colors particularly RGB, the minimum value is 0 and maximum is 255.
With hexadecimal numbers we work with 3 pairs of Red, Green and Blue, hence RGB.
_ _  _ _  _ _
R     G     B

The minimum pair is 00 and maximum FF. Oh! Letters? Yes, I will show you in a bit how this works. As I said above the decimal numbers are limited to 0-9, so a few engineers had to figure out a way to solve the problem. To get things working they assigned the following values:

A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

Few examples:

Red: (15x16) + (15x1) = 255
Green: (0x16) + (0x1) = 0
Blue: (0x16) + (0x1) = 0
Yes, you got it right, the color is full red! My favorite color.

Red: (8x16) + (3x1) = 131
Green: (13x16) + (9x1) = 217
Blue: (0x16) + (0x1) = 0

Red: (14x16) + (15x1) = 239
Green: (11x16) + (11x1) = 187
Blue: (3x16) + (10x1) = 58

And that’s it! You know how to calculate hexadecimal number and how they are related to numbers.

What are Programming Languages ?

2 weeks ago I went to an Equal Experts Conference and after while mingling, in a conversation with one of the organizers, some were saying that language X was better than language B, and his response was: “Languages are just tools to build applications to serve the world”. That just killed.

And this is the message about this post, Programming Languages are just tools, you use them and not get used by them.

Have a nice day.