Solving Algorithms – Largest prime factor

Today I solved this algorithm that is the third problem in Project Euler.
This still need some improvement, because even when the last prime number is found it still counts and takes a really long time when given big numbers.

require 'prime'

def largestPrime(num)
  result = 0
  (2...num).each do |i|
    result = i if (num % i) == 0 && i.prime?
  end
  puts result
end

largestPrime(13195)

I will update this code as soon as I find a better solution but for now it works.

Design Patterns

I’ve got this book last week. It’s the most expensive I’ve ever bought and I do not regret!
I am still in the beginning and learned what are Design Patterns, the catalog of the 23 design patterns in this book and how they solve problems. After, will be shown how to select and use a design pattern, a case study and then we delve into each one of the patterns. The authors explain Object Oriented Design and how objects interact with each other as an introduction.

IMG_20160726_143709727

The good thing about this is that the book has a guide and is suited for rookie readers which is awesome!

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?")
  end
end

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)
  end
  title = words.join(' ')
end

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
  false
  elsif arr.permutation(2).any? { |a, b| a + b == n }
    true
  else
    false
  end
end

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.

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.