Hi, I found a nice programming challenge:
http://acm.uva.es/p/v3/333.html

I have written an entry in Ruby, and I'd like to know if there are
things I could improve.


#!/usr/bin/env ruby

class ISBN
  def initialize(code)
    @code = code
  end

  def calc(lst)
    ans = []
    ans[0] = lst[0]
    for i in 1...lst.length
      ans[i] = ans[i-1] + lst[i]
    end
    ans
  end

  def validChars
    stripCode = @code.tr("-", "")
    if stripCode =~ /^[0-9]{9}[0-9X]$/i
      true
    else
      false
    end
  end

  def validate
    if validChars then
      stripCode = @code.tr("-", "")
      s0 = []
      stripCode.each_byte { |c|
        s0 << c.chr
      }
      s0[-1] = 10 if s0[-1] =~ /x/i
      s0.map! { |x| x.to_i }
    else
      raise "Invalid ISBN"
    end

    s1 = calc(s0)
    s2 = calc(s1)

    raise "Invalid ISBN" if s2[-1] % 11 != 0
  end
end

while true
  print "> "
  code = gets.strip
  begin
    isbn = ISBN.new(code)
    isbn.validate
    puts "#{code}: Valid ISBN"
  rescue Exception => e
    puts e
  end
end


-- 

Vincent Foley-Bourgon
Email: vinfoley / iquebec.com
Homepage: http://darkhost.mine.nu:81