Created
February 5, 2013 12:36
-
-
Save rajeevkannav/4714198 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#################################### Solution #################################### | |
#### Module Rulecheck #### | |
module Rulecheck | |
TO_BLOCK = ['!'] | |
def parse(string) | |
TO_BLOCK.each do |to_block| | |
return string.include?(to_block) | |
end | |
end | |
end | |
#### Class Available Gift Options #### | |
class Gift | |
@@gifts = [] | |
attr_accessor :name, :value | |
def initialize(name, value) | |
@name, @value = name, value | |
@@gifts << name | |
@@gifts << value | |
end | |
def self.gifts | |
Hash[*@@gifts] | |
end | |
end | |
#### Class Coupon For Users #### | |
class Coupon | |
@@coupons = [] | |
attr_accessor :amount, :options | |
def initialize(amount, options = []) | |
@@coupons << amount | |
@@coupons << options | |
end | |
def self.coupons | |
Hash[Hash[*@@coupons].to_a.reverse] | |
end | |
end | |
class Input | |
include Rulecheck | |
def get_gifts | |
puts "Enter the Number of gifts Type :- " | |
types = gets.chomp.to_i | |
types.times do |i| | |
puts "Enter #{i}th gift Type with Credit :- " | |
new_gift ||= gets.chomp.split(' ') | |
gift = Gift.new(new_gift[0],new_gift[1]) | |
end | |
end | |
def initialize | |
get_gifts | |
end | |
def get_coupon | |
puts "Enter the Your Amount and Coupon Options :- " | |
coupon = gets.chomp.split(' ') | |
coupon = Coupon.new(coupon.first,coupon.drop(1)) | |
Coupon.coupons | |
end | |
def evaluate_delivery(gifts) | |
gifts = Hash[gifts.sort_by {|k,v| v}.reverse] | |
while true | |
coupon = get_coupon | |
@options = [] | |
coupon.values.first.each do |cv| | |
@options << cv | |
@options << gifts["#{cv}"] | |
end | |
amount = coupon.keys.first.to_i | |
sorted_options = coupon["#{amount}"].sort{|x,y| gifts[y].to_i <=> gifts[x].to_i } | |
deliverables = deliver(amount,sorted_options,gifts) | |
puts deliverables.inspect | |
end | |
end | |
def deliver(amount,sorted_options,gifts) | |
deliverables = [] | |
sorted_options.each do |option| | |
if amount >= gifts["#{option}"].to_i | |
deliverables << option unless parse(option.to_s) | |
amount -= gifts["#{option}"].to_i | |
end | |
end | |
deliverables | |
end | |
end | |
input = Input.new | |
gift_list = Gift.gifts | |
input.evaluate_delivery(gift_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment