Created
May 3, 2011 19:21
-
-
Save defkode/954011 to your computer and use it in GitHub Desktop.
lotto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Lottery | |
attr_reader :lots_count, :balls_count | |
def initialize(lots_count, balls_count) | |
@lots_count = lots_count | |
@balls_count = balls_count | |
end | |
def full_basket | |
Array.new(balls_count) {|i| i + 1} | |
end | |
def get_lucky_numbers | |
basket = full_basket | |
lucky_numbers = [] | |
lots_count.times do | |
ball = mix(basket) | |
basket = pick_ball(ball, basket) | |
lucky_numbers.push(ball) | |
end | |
lucky_numbers.sort | |
end | |
# returns numbers that are present in both arrays | |
def compare_results(result_one, result_two) | |
unless result_one.size == lots_count || result_two.size == lots_count | |
raise ArgumentError, "both arrays must have #{lots_count} elements" | |
end | |
result_one & result_two | |
end | |
protected | |
# picks ball from basket and returns updated basket | |
def pick_ball(ball, basket) | |
basket.delete(ball) | |
basket | |
end | |
# gets a lucky ball | |
def mix(basket) | |
basket[rand(basket.length)] | |
end | |
end | |
class Lotto < Lottery | |
def initialize | |
super(6, 49) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment