Lotto checking script

Feb 12
2008

The idea comes from http://www.waider.ie/hacks/workshop/perl/lotto.pl script.

Should I be so wasteful? No:

irb

my_numbers       = [1,2,3,4,5,6]
winning_numbers = [1,2,3,41,42,43]
 
result = 6 - (winning_numbers - my_numbers).size
 
puts "#{result} " +
  if not result.eql? 0
    "hits"
  else
    "hit"
  end

String Iterator

Jan 14
2008
class String
  class Iterator < String
 
    def initialize(str='', next_char=0)
      @next_char = next_char
      super(str)
    end
 
    def next
      return nil if @next_char > self.size
 
      @next_char += 1
 
      get_char(-1)
    end
 
    def prev
      return nil if @next_char < = 0
 
      @next_char -= 1 if @next_char > 0
 
      get_char()
    end
 
    private
    def get_char(pos=0)
      if block_given?
        yield split(//)[@next_char+(pos)]
      else
        split(//)[@next_char+(pos)]
      end
    end
  end
end
 
s = String::Iterator.new "abcdef"
 
while char = s.next
  puts char
end
 
while char = s.prev
  puts char
end

Calendar

July 2010
M T W T F S S
« Apr    
 1234
567891011
12131415161718
19202122232425
262728293031  

Tags