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.

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
    end
  end
  break if !swapped
end

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

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) }
  end
  @values
end

Test Driven Development

In my last post I shared the difficulty of Automated Testing and skipped that exercise, then stumbled across Ruby Koans by Neo, which is a way of learning Ruby syntax through TDD. I began on Friday and continued until today, minus the weekend because it’s almost fully occupied by work. I’m half way on it and has proven me to be a very effective way of learning the language and testing as well. After finishing I will take Michael Hartl’s Rails Tutorial book.

This blog post should have been written last week, so this week I will write two, more technical maybe about Python to remember a few things and also to keep the momentum.

Object Oriented Programming was a lot abstract for me (and still is), I was accustomed with Pascal and C (which I love) but thanks to Ruby it is going away.

Back on Track

Well I have been on and off programming after leaving College.
This was rekindled after seeing my girlfriend in late 2015 doing a SQL tutorial in Khan’s Academy, which I completed in a few days.
Then restarted LPTHW book, but changed to LRTHW which I began in late in 2014 and never went back to it. I am currently in the 48th exercise and having a hard time with it. This exercise is about building a code to pass an Automated Testing done by the author named Zed A. Shaw. It is so far the hardest for me besides the one with dictionaries that made me quit twice. If I only had opened the terminal and wrote the code in a shell that wall of fire could be overcome easily.
Just to say that pushing through the exercises is making me tougher and ready to conquer the Software Development Industry in the long run. Gotta be bold!

Perhaps it’s good for today and will return tomorrow with full force.