Categories
ruby

Summing up factorial

Another way for factorial, extending the integer class, so the function will feel more natural, instead of fact 6 for example, you just call it by 6.fact.
Also, added a function to sum up the factorial value, counting down. Example, for 6!, it will be 6! + 5! + 4! + 3! + 2! + 1!
Here go,

class Integer
  def fact
    if self == 0
      return 1
    else
      n = self * (self - 1).fact
    end
  end
  def sum_fact
    (1..self).inject{|a,b| a + self.fact}
  end
end
puts 4.fact
puts 4.sum_fact
Categories
ruby

failing factorial

Hours ago I got a programming test question about factorial, which I didn’t do really well, only completed with the help of the interviewer. Damn.
The worst thing is that I have done this question before, and I know in the head I can use recursion for it, but I totally go blank on how to do it with recursion. Here go my chance…sigh…

Now, here is how factorial can be done.
With Recursion.

def fact n
  if n == 0
    1
  else
    n = n * fact(n-1)
  end
end

That simple.

Factorial without recursion, using inject instead

def fact n
  if n == 0
    1
  else
    (1..n).inject { |a,b| a*b } 
  end
end

Isn’t that difficult, huh? What was I thinking?!

Categories
ruby on rails

clearing rails console

And so not to forget this again.
This is how to clear the console screen in rails, type this,
In Ubuntu,

system 'clear'

In Windows

system 'cls'

from stackoverflow

Categories
ruby

My sort of sort

Am running through “Learn to Program” from Chris Pine, again.
I have been cracking my head on the exercise to write a sort method instead of just using the array#sort come with ruby.

As the exercise actually came at the stage of the tutorial that more advanced techniques like ternary operator is yet to be introduced, so I figure I might want to limit myself to use only the basic while and if.There are some blog posts, discussion in forums on the solution to this question.

Someone suggest to read about sorting algorithm before attempting to solve the question, a brief search yields all sorts of sort (ha!), insertion, selection, bubble, shell, merge, heap, quick, just for sorting. As sorting always require brain gymnastic trying to imagine the on going process (at least for me), there are animations available for all these different type of sort. Although, frankly I still couldn’t really get a grasp after watching those animations.

As I read from Renae’s blog that they are using paper cutouts, while I have been sketching and sketching and sketching trying to illustrate the flow.

And so, here is my attempt to the problem, it is rather coarse, it didn’t use method, it can’t deal with duplicate elements in the array, so, there are still a lot of be improve on, when I have time on it. Nevertheless, here it is

animals = ['cat','bird','a','mouse','bull','elephant','dog']
unsorted = animals.dup
sorted= []
i = animals.length - 1
smaller = animals[i]
 
while unsorted.length != 0
   while i >= 0
    if smaller < animals[i]
      smaller
    else
      smaller = animals[i]
    end
    i = i - 1
 
  end
 
    sorted.push smaller
    unsorted.delete smaller
    animals.delete smaller
    smaller = unsorted[i] 
    i = unsorted.length - 1   
end
    puts sorted.inspect
Categories
ruby

Sorting Array

a = [9,5,6,3,7]
a.sort { |x,y| x==5? 1:x<=>y }
[3, 6, 7, 9, 5]

So, I sort the array consisting 9,5,6,3,7, run it through a block, put the elements to x and y then sort it in ascending order, except for the number “5”, which will be stuck at the end of the order.

But why in the case below then the number “2” which should be at the end of the order is instead at the second last place?

c = [22,77,88,55,44,11,2,8]
c.sort { |x,y| x==2? 1:x<=>y }
[8, 11, 22, 44, 55, 77, 2, 88]

I tried to sort a few different arrays, and sometimes the second case just pop up, which still puzzling me. Why the different?

Categories
ruby

Array rediscovered

And so I always understand it wrongly,

1
2
s = "hello"
s[0,2] = "He"

That I thought the second operand is the ending index, and that somehow it works like range, where

1
2
a = [1,2,3]
a[0...2] = [1,2]

So, the last index is excluded from the result, it ranged from index 0 up till index 2, but index 2 itself is not included.

Only now I know that the second operand in retrieving substring from an array is actually the length that we specified for the substring, and the first operand is the starting index.

So, it now make more sense for me.

Categories
ruby

Pik for windows

So glad when I come across rvm, but then I learned that it is only for Linux and OSX. I do work in Ubuntu sometimes, but other time I have to work in a Windows environment.

Then, I discover pik, hey, doing the same thing for you, but in Windows. So now even in XP, I can switch around with Ruby versions as well.

Installation is really easy, just gem install pik,

gem install pik

then you can tell pik the additional ruby versions by adding the path to it.

C:\pik add C:\ruby186\bin

To see all the versions you have

C:\>pik list -v
186: ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
path: C:/ruby186/bin

191: ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mingw32] *
path: C:/Ruby19/bin

And do the switching,

C:\pik switch 186

More commands can be found at the pik site.

Rediculuosly simple yet so helpful….thanks Gordon!

Categories
git

Git the Terminator

With all the branching, go back and forth between future and the past, shouldn’t someone already start using Terminator as an tutorial for learning Git?

terminator_timeline

from terminator.wikia.com

Categories
ruby on rails

Import Excel data with FasterCSV

So I have a whole list of data which is already in excel and need to put into the Rails application. I could either manually type them into the database, or there must be some plugin out there dealing with this.

Just a quick google bring me to Unixmonkey blog talking about fastercsv.

All you need to do is just

gem install fastercsv

Then copy the excel file(oh, convert it to .csv file first) to the root of your application.

Now as the target table is “Bcode” with fields, term_no, standard_term  at the root, ruby script/console

require 'fastercsv'
FasterCSV.foreach("#{RAILS_ROOT}/file.csv") do |row|
  record = Bcode.new(
  :term_no => row[0],
  :standard_term => row[1]
  )
  record.save
end

That is just a few simple line of codes, but believe me, it got me almost 5 times of trying before I got everything right in the console. It is just so frustrated. And only after all that suffering that I find the line saying we can actually put this into data migration script. Arghhh…..

It did take a while (almost 10 minutes, I think) to load in all the 17K rows of cvs data though.

Categories
ruby on rails

Rails collection_select helper

The explanation from api.rubyonrails.org is rather baffling for a newbie like me,

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

Still managed to get the dropdown menu working for my application after searching through the web. There are already quite a few useful resources available in the net for helper method collection_select.

But I seem to lost in the code again looking back at it after a while. And am too lazy to look through all the stuff again. So I try to make a diagram to put down what I already understand from those resources. Might not be the best or complete explanation, but just try to help myself to be able to view it and quickly get a grasp of it.
collection_select_explain_3