Monthly Archives: August 2008

I was wondering why the integer class in Ruby lacked a method like octal, so i guess it would be nice to throw one in myself to make things easier.

class Integer

    def octal
        require ‘binary’
        num = self
        num_to_binary = num.binary
        array = %w(0 00)

        if num.size % 3 != 0
            array.each do |obj|
                concat_str = obj + num_to_binary
                if concat_str.size % 3 == 0
                    num_to_binary = concat_str
                end
            end
        end

        octal_mappings = {
             ‘000′ => ‘0′,
             ‘001′ => ‘1′,
             ‘010′ => ‘2′,
             ‘011′ => ‘3′,
             ‘100′ => ‘4′,
             ‘101′ => ‘5′,
             ‘110′ => ‘6′,
             ‘111′ => ‘7′
        }

        split_str = num_to_binary.scan(/…/)
        hex_array = []

        split_str.each do |str|

            hex_array.push octal_mappings[str]

        end

        hex_array.to_s
    end
end

Hey, do you want to view all the possible arrangement of the letters of a word, then try this.

 

#   file   : wordperm.rb

#   Author : Selasie Agbavor <volsbit@gmail.com>

#   Date   : May, 16 2008

#   Time   : 14:20:00 G.M.T

 
class String

   def shuffle

       self.split(//).sort_by{rand}.to_s

   end

end
class Integer

    def fact

        if self.zero?
            1
        else
            self * (self-1).fact
        end
    end

end
word = gets.chomp

array_of_letters = word.split(//)

hash = Hash.new(0)
array_of_letters.each{ |lett|

    hash[lett] += 1

}

counts_greater_than_one = []

hash.each_value{ |val|

    counts_greater_than_one << val if val > 1

}

if !counts_greater_than_one.empty?

    product = 1
    counts_greater_than_one.each{ |num|

        product *= num.fact

    }

    permutation = word.size.fact / product
   
else

    permutation = word.size.fact
   
end

 

already_shuffled_words = []
count = 0

while count < permutation

    shuffled_word = word.shuffle

    if !already_shuffled_words.include? shuffled_word

        puts shuffled_word
        #sleep 1  # optional
        already_shuffled_words << shuffled_word
        count += 1

    end

end

puts “\n”

puts “#{word} can be arranged #{permutation} ways taking all characters”

looking for a library to do some basic statistics computations, check this out, then.

#  Author : Selasie Agbavor <volsbit@gmail.com>

#  Source : Ruby 

 

class Array
  def even?
    self.length % 2 == 0
  end

  def sum
    sum = 0
    self.each { |i| sum += i }
    sum
  end
end
class Stats
  def initialize(*data)
    @data = data.map { |s| s.to_i}
    @size = data.length
    variance
  end

  def size
    @size
  end

  def data
    @data
  end

  def mean
    mean = @data.sum / @size.to_f
  end

  def mode
    frequencies = Hash.new(0)
    @data.each{ |i| frequencies[i] += 1 }
    highest_frequency = frequencies.values.max
    frequencies.keys.select { |key| frequencies[key] == highest_frequency }
  end

  alias :average :mean

  def median
    max_index = @size – 1
    sort_data = @data.sort
    if @data.even?
      mid_1 = max_index / 2
      mid_2 = mid_1 + 1
      (sort_data[mid_1] + sort_data[mid_2]) / 2
    else
      sort_data[(max_index + 1) / 2]
    end
  end

  def variance
    devi_set = @data.map { |datum| datum – mean }
    sum = devi_set.map{ |datum| datum * datum}.sum
    @variance = sum / @size #  or @size – 1
  end

  def standard_deviation
    Math.sqrt(@variance)
  end

  def range
    @data.max – @data.min
  end

end

#  create a data object like so :

#  my_school_data = Stats.new(2, 3, 4, 5, 6,  7)

#  puts my_school_data.mean => 4.5

#  puts my_school_data.standard_deviation => 1.70782512765993