Saari Development

Ruby : Deep Copy

Posted in code, ruby by imsaar on January 9th, 2008

# problem
a = [[0, 1], [10], 19]
b = a.dup
b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 100], [10], 19]

# solution
a = [[0, 1], [10], 19]
b = Marshal.load(Marshal.dump(a))
b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 1], [10], 19]

Ruby: Grid Computing Golf

Posted in code, golf, ruby by imsaar on January 8th, 2008

My first attempt at really playing code golf, that is solving a problem in minimum characters of code and I did not do very well (200 character compared to the best 43 perl or 63 ruby characters).
http://codegolf.com/grid-computing


m = []
r = []
c = Array.new(10, 0)
0.upto(9) {|n|m<<gets.chomp.split.map {|x|x.to_i};r<<m[n].inject(0){|s,e| s += e}}
0.upto(9) {|x|m.each_with_index{|v,z|c[x] += v[x]}}
puts [r.max, c.max].max

Update:
I further squeezed some white-space out of the code and it is starting to look uglier (now it is 184 characters). I am not sure I like the code golf concept although I like to solve coding problems.


m=[]
r=[]
c=Array.new(10,0)
0.upto(9){|n|m<<gets.chomp.split.map{|x|x.to_i};r<<m[n].inject(0){|s,e|s+=e}}
0.upto(9){|x|m.each_with_index{|v,z|c[x]+=v[x]}}
puts [r.max,c.max].max

Ruby: Oblongular Number Spirals Solution

Posted in code, ruby by imsaar on January 4th, 2008

<pre>
  # Oblongular Number Spirals
  # http://codegolf.com/oblongular-number-spirals

  class Spiral
    attr_accessor :row_num, :col_num, :direction

    def initialize(m, n)
      @spiral = []
      @value =
     @row_num = m
     @col_num = n
     @direction = :right
   end

   def get(row, col)
     if (row = row_num || col = col_num)
         raise RangeError.new("Out of Bound : row = #{row} col = #{col}")
     end
     @spiral[row] ||= Array.new
     @spiral[row][col] ||= nil
     @spiral[row][col]
   end

   def increment_fill(row, col)
     @value +=
     fill(row, col, @value)
   end

   def fill(row, col, value)
     if (row = row_num || col = col_num)
       raise RangeError.new("Out of Bound : row = #{row} col = #{col}")
     end
     @spiral[row] ||= Array.new
     @spiral[row][col] = value
   end

   def populate(x =, y =)
     count =
     while ()
       increment_fill(x, y)
       count +=
       # exit the loop once iteration == total_cells
       break if count == row_num * col_num
       x, y = next_cell(x, y)
     end
   @spiral
   end

   def next_cell(row, col)
     new_row = row
     new_col = col

     case @direction
     when :right
       new_col = col + 1
     when :left
       new_col = col - 1
     when :down
       new_row = row + 1
     when :up
       new_row = row - 1
     end

     if ((new_col >= col_num || new_col = row_num || new_row < 0))
       change_direction
       next_cell(row, col)
     elsif (get(new_row, new_col) != nil)
       change_direction
       next_cell(row, col)
     else
       return [new_row, new_col]
     end
   end

   def change_direction
     case @direction
     when :right
       @direction = :down
     when :down
       @direction = :left
     when :left
       @direction = :up
     when :up
       @direction = :right
     else
       raise ArgumentError.new("Illegal value  for current_direction #{@direction}")
     end
   end

   def dump
    display = ''
    @spiral.each do |row|
      row.each do |col|
        display << sprintf("%d ", col)
      end
      display << "\n"
    end
    puts display
   end
 end

 if $ == __FILE__
   s = Spiral.new(ARGV[].to_i, ARGV[].to_i)
   s.populate
   s.dump
 end

# test file below
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')

  require 'test/unit'
  require 'spiral'

  class TestSpiral &lt; Test::Unit::TestCase
    def setup
      @spiral = Spiral.new(,)
    end

   def test_increment_fill_0_0_first
     @spiral.increment_fill(0, 0)
     assert_equal(1, @spiral.get(0, 0))
   end

   def test_increment_fill_1_1_first
     @spiral.increment_fill(1, 1)
     assert_equal(1, @spiral.get(1, 1))
   end

   def test_increment_fill_successive
     @spiral.increment_fill(0, 0)
     @spiral.increment_fill(0, 1)
     @spiral.increment_fill(1, 1)
     assert_equal(3, @spiral.get(1, 1))
   end

   def test_get_beyond_limit_raises_exception
     assert_raise(RangeError) {
       @spiral.get(@spiral.row_num + 1, @spiral.col_num + 1)
     }
   end

   def test_get_below_zero_raises_exception
     assert_raise(RangeError) {
       @spiral.get(-,)
     }
   end

   def test_get_at_limit_raises_exception
     assert_raise(RangeError) {
       @spiral.get(@spiral.row_num, @spiral.col_num)
     }
   end

   def test_unfill_get_with_in_range_does_not_raise_exception
     assert_nothing_raised() {
       @spiral.get(@spiral.row_num -, @spiral.col_num - 1)
     }
   end

   def test_unfill_get_with_in_range_returns_nil
     assert_equal(nil, @spiral.get(@spiral.row_num - 1, @spiral.col_num - 1))
   end

   def test_fill_beyond_range_raises_exception
     assert_raise(RangeError) {
       @spiral.increment_fill(@spiral.row_num + 1, @spiral.col_num + 1)
     }
   end

   def test_fill_beyond_range_raises_exception
     assert_raise(RangeError) {
       @spiral.increment_fill(0, -1)
     }
   end

   def test_fill_at_range_raises_exception
     assert_raise(RangeError) {
       @spiral.increment_fill(@spiral.row_num, @spiral.col_num)
     }
   end

   def test_populate
     spiral = [
           [1, 2, 3, 4, 5],
           [14, 15, 16, 17, 6],
           [13, 20, 19, 18, 7],
           [12, 11, 10, 9, 8]
          ]
     s = @spiral.populate
     assert_equal(spiral, s)
   end

   def test_change_directon_right
     @spiral.direction = :right
     @spiral.change_direction
     assert_equal(:down, @spiral.direction)
   end

   def test_change_directon_up
     @spiral.direction = :up
     @spiral.change_direction
     assert_equal(:right, @spiral.direction)
   end

   def test_next_cell_0_0
     x, y = @spiral.next_cell(0,0)
     assert_equal(0, x)
     assert_equal(1, y)
   end

   def test_next_cell_0_4
     x, y = @spiral.next_cell(0,4)
     assert_equal(1, x)
     assert_equal(4, y)
   end

 end
</pre>

Ruby : Time Math Interview Problem With Bug Fixed (still writing test first)

Posted in code, ruby by imsaar on January 2nd, 2008

While discussing my friend Arsalan’s C# (seemlessly compiled on my linux machine using mcs)
solution and testing it out I found a bug in my own code that. The problem was when adding
more than 12 hours (> 720 minutes) it was not doing the right thing. The code can still be
refactored for cleaner solution but it is too late at night to do that now. Also, my wife
gave me another idea to convert the time to minutes before adding which I will try out later.


   # Without using any built in date or time functions, write a function or method
   # that accepts two mandatory arguments. The first argument is a string of the
   # format "[H]H:MM {AM|PM}" and the second argument is an integer. Assume the
   # integer is the number of minutes to add to the string. The return value of
   # the function should be a string of the same format as the first argument.
   # For example AddMinutes(" AM",) would return " AM". The exercise
   # isn't meant to be too hard. I just want to see how you code. Feel free to
   # do it procedurally or in an object oriented way, whichever you prefer. Use
   # any language you want. Write production quality code.
  # Question Source: http://blist.com/blog/

  # the following solution was developed using TDD

  require 'test/unit'

  class TestTimeCalc < Test::Unit::TestCase

      def setup
          @time = " AM"
      end

      def test_new_time_cal
          assert_not_nil(TimeCalc.new)
      end

      def test_add_minute_zero
          assert_equal(@time, TimeCalc.add_minutes(@time,))
      end

      def test_add_minute_ten
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_minute_thirteen
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_hour
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_two_hours_fifteen_minutes
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_past_meridiem
          # minutes = hours and minutes
          assert_equal(" PM", TimeCalc.add_minutes(@time,))
      end

      def test_alpha_hour_min_format_throws_exception
          assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD AM",) }
      end

      def test_bad_meridiem_throws_exception
          assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD TM",) }
      end

      def test_hr_greater_than_twelve
          assert_raise(ArgumentError) { TimeCalc.add_minutes(" PM",) }
      end

      def test_min_greater_than_fifty_nine
          assert_raise(ArgumentError) { TimeCalc.add_minutes(" PM",) }
      end

      def test_add_lot_of_minutes
        # minutes = hours and minutes
        assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_up_to_noon
        # AM plus hr min ()
        assert_equal(" PM", TimeCalc.add_minutes(@time,))
      end

      def test_add_whole_lot_of_minutes
        # minutes = hours and minutes
        assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

  end # end class TestTimeCalc

  class TimeCalc

      def self.add_minutes(time, minutes)
        (hour, min, meridiem) = parse_time_string(time)

        hour_increment = (min + minutes)/
        min_increment = (min + minutes)% - min

        while (hour + hour_increment >)
          meridiem = (meridiem == 'AM' ? 'PM' : 'AM')
          hour_increment -=
        end

        hour += hour_increment
        # special case forth hour
        meridiem = (meridiem == 'AM' ? 'PM' : 'AM') if hour ==
        min += min_increment

        hour.to_s + ":" + sprintf('%d', min) + " " + meridiem
      end

      private

      def self.parse_time_string(time)
          raise ArgumentError unless (matches = time.match(/(\d{,}):(\d{,})\s+(\w{})/))
          matches = time.match(/^(\d{,}):(\d{,})\s+([A|P]M)$/)
          hour = matches[].to_i
          min = matches[].to_i
          meridiem = matches[]
          raise ArgumentError unless (hour <=)
          raise ArgumentError unless (min <)
          return [hour, min, meridiem]
      end
  end # end class TimeCalc

 if __FILE__ == $
   puts TimeCalc.add_minutes(ARGV[], ARGV[].to_i)
 end

Ruby: One liner to strip the line number from code posted

Posted in code, ruby by imsaar on December 31st, 2007

In response to my previous post my friend Arsalan posted his C# solution on my shared friend blog SaarayDost

Both my solution and his had line numbers with the code which makes it easy to reference a line number in a discussion but if you want to run and test code it can be painful to strip them out one by one.

I wrote the following quick ruby one liner that can be run on a file after copy-pasting the code and saving it.


ruby -lne 'puts $_.gsub(/^\s?\d+:?/, "")' ali.rb > ali_fixed.rb

I am sure there good be a better more clever way to do this after all TIMTOWDI but here I use the l to remove newline at the end of each line, n to iterate over the given file one line at a time and e to execute the following code string (in quotes, on each line). $_ is the magic variable (a perl legacy in ruby) that magically populated, in this case with the current line of file being processed. gsub simply removed the pattern match in the first argument by the string in the second argument.

While searching for a good description of $_ I also found this interesting link with other useful ruby one-liners.

Update: found another way to achieve the above result. -p option prints the $_ at the end of each iteration.


ruby -lpe '$_.gsub!(/^\s?\d+:?/, "")' ali.rb > ali_fixed.rb

Ruby : Time Math Interview Problem Done Test First

Posted in code, ruby by imsaar on December 28th, 2007

Question from : Blist.com Career Page


 # Without using any built in date or time functions, write a function or method
 # that accepts two mandatory arguments. The first argument is a string of the
 # format "[H]H:MM {AM|PM}" and the second argument is an integer. Assume the
 # integer is the number of minutes to add to the string. The return value of
 # the function should be a string of the same format as the first argument.
 # For example AddMinutes("9:13 AM", 10) would return "9:23 AM". The exercise
 # isn't meant to be too hard. I just want to see how you code. Feel free to
 # do it procedurally or in an object oriented way, whichever you prefer. Use
 # any language you want. Write production quality code.
 # Question Source: http://blist.com/blog/

 # the following solution was developed using TDD

 require 'test/unit'

 class TestTimeCalc < Test::Unit::TestCase

     def setup
         @time = "9:13 AM"
     end

     def test_new_time_cal
         assert_not_nil(TimeCalc.new)
     end

     def test_add_minute_zero
         assert_equal(@time, TimeCalc.add_minutes(@time, 0))
     end

     def test_add_minute_ten
         assert_equal("9:23 AM", TimeCalc.add_minutes(@time, 10))
     end

     def test_add_minute_thirteen
         assert_equal("9:26 AM", TimeCalc.add_minutes(@time, 13))
     end

     def test_add_hour
         assert_equal("10:13 AM", TimeCalc.add_minutes(@time, 60))
     end

     def test_add_two_hours_fifteen_minutes
         assert_equal("11:28 AM", TimeCalc.add_minutes(@time, 135))
     end

     def test_add_past_meridiem
         # 785 minutes = 13 hours and 5 minutes
         assert_equal("10:18 PM", TimeCalc.add_minutes(@time, 785))
     end

     def test_alpha_hour_min_format_throws_exception
         assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD AM", 10) }
     end

     def test_bad_meridiem_throws_exception
         assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD TM", 10) }
     end

     def test_hr_greater_than_twelve
         assert_raise(ArgumentError) { TimeCalc.add_minutes("13:00 PM", 10) }
     end

     def test_min_greater_than_fifty_nine
         assert_raise(ArgumentError) { TimeCalc.add_minutes("12:60 PM", 10) }
     end

 end # end class TestTimeCalc

 class TimeCalc

     def self.add_minutes(time, minutes)
       (hour, min, meridiem) = parse_time_string(time)

       hour_increment = (min + minutes)/60
       min_increment = (min + minutes)%60 - min
       if (hour_increment >= 12)
         meridiem = (meridiem == 'AM' ? 'PM' : 'AM')
         hour_increment -= 12
       end

       hour += hour_increment
       min += min_increment

       hour.to_s + ":" + min.to_s + " " + meridiem
     end

     private

     def self.parse_time_string(time)
         raise ArgumentError unless (matches = time.match(/(\d{1,2}):(\d{1,2})\s+(\w{2})/))
         matches = time.match(/^(\d{1,2}):(\d{1,2})\s+([A|P]M)$/)
         hour = matches[1].to_i
         min = matches[2].to_i
         meridiem = matches[3]
         raise ArgumentError unless (hour <= 12)
         raise ArgumentError unless (min < 60)
         return [hour, min, meridiem]
     end

 end # end class TimeCalc

Ruby: net/smtp weirdness - to address can’t contain dash/hyphen (-)

Posted in code, ruby by imsaar on October 3rd, 2007

This caused me a lot of grief and debugging time.

I am using code like this to send emails from a CGI script:


    Net::SMTP.start('smtp.example.com', 25) do |smtp|
      smtp.open_message_stream('from@example.com', ['dest-address@example.com']) do |f|
        f.puts 'From: from@example.com'
        f.puts 'To: dest-address@example.com'
        f.puts 'Cc: cc-address@example.com'
        f.puts 'Subject: test message'
        f.puts
        f.puts 'This is a test message.'
      end
    end

and this is failing silent in my case.

I have discovered through trial and error that if I remove the -(dash) from the to address it works. Dashes in from or CC addresses does not matter.

Ruby: Rotating Image Files Code

Posted in code, ruby by imsaar on June 25th, 2007

require 'rmagick'
require 'exifr'
require 'pp'

ROTATE_CLOCKWISE = 90
ROTATE_ANTICLOCKWISE = -90

def rotate(file)
  image_exif = EXIFR::JPEG.new(file).exif

  puts "#{file} : #{image_exif[:orientation]}"
  if (image_exif[:orientation] == EXIFR::TIFF::RightTopOrientation)
    degrees = ROTATE_CLOCKWISE
  elsif (image_exif[:orientation] == EXIFR::TIFF::LeftBottomOrientation)
    degrees = ROTATE_ANTICLOCKWISE
  else
    puts "Not rotating #{file}"
    return
  end

  image = (Magick::Image.read(file)).first
  #image.rotate(degrees).write("#{file.split('.').first}_rotated.jpg")
  image.rotate(degrees).write("#{file}")
end

dir = ARGV[0]

Dir.entries(dir).each do |file|
  if (file =~ /jpg/i)
    rotate(File.join(dir, file))
  end
end