Friday, August 17, 2012

Ruby programming: check lottory numbers


This script checks the lottery tickets against the Winning Numbers.

*** ABSOLUTELY NO WARRANTY AND NO LIABILITY. The program is for fun purpose only. Not for use in real lottery ticket checking. ***

#!/usr/bin/env ruby

class WinningNumbers

   attr_reader :numbers

   def initialize()
      @numbers = nil
   end

   def set(numbersString)
      @numbers = numbersString.split(' ')
      print("Winning Numbers: " )
      @numbers.each { |number| print(number, " ") }
      print("\n" )
   end

end

class Group

   def initialize(numbersString)
      @numbers = numbersString.split(' ')
   end

   def check(winningNumbers)
      print("Group: ")
      @numbers.each { |number| print(number, " ") }
      print(" ==> ")

      @numbers.each do |number|
         winningNumbers.numbers.each do |winNumber|
            if winNumber == number
               print(number, " ")
            end
         end
      end

      # check bonus
      @numbers.each do |number|
         winningNumbers.numbers.each do |winNumber|
            bonusNum = "(" + number + ")"
            if winNumber == bonusNum
               print(bonusNum, " ")
            end
         end
      end

      print("\n")
   end

end

if __FILE__ == $0

   winningNumbers = WinningNumbers.new

   if !ARGV[0]
      puts("Usage: #{$0} <lotto file with Winning Numbers in the 1st line>")
      exit
   end

   file = File.open(ARGV[0])

   file.each do |line|

      case line
         when /Winning Numbers[:\s]\s*(.+)/
            winningNumbers.set($1)
  
         when /\S+/
            group = Group.new(line)
            group.check(winningNumbers)
      end

   end

end

### ABSOLUTELY NO WARRANTY AND NO LIABILITY. The program is for fun purpose only. Not for use in real lottery tickets checking. ###

To play with it, copy and paste the above code to a file and save it. (In Linux) change the file's permission to executable. Put the winning numbers and the bought numbers into a file, e.g.

Winning Numbers: 01 02 03 04 05 06 07 (08)

03 04 11 12 13 14 15
01 03 08 09 10 11 12
11 12 13 14 15 16 17

And then run it (e.g. the script file is named lottery.rb and the numbers are in file lottery_numbers):
$ ./lottery.rb lottery_numbers
The result of the above example is:
Winning Numbers: 01 02 03 04 05 06 07 (08) 
Group: 03 04 11 12 13 14 15  ==> 03 04 
Group: 01 03 08 09 10 11 12  ==> 01 03 (08) 
Group: 11 12 13 14 15 16 17  ==> 
 
### ABSOLUTELY NO WARRANTY AND NO LIABILITY. The program is for fun purpose only. Not for use in real lottery tickets checking. ###
 

2 comments:

Australian lotto results said...

A good idea! I'll try it!

Unknown said...

Hello!
Such a nice idea! I read carefully your article, but I can't do it without experts. Please help me to check lottery numbers in this winning Australian lotto results

I hope I got some odds to win this lottery. Finally, my money needs to be climbed.

 
Get This <