Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions Training/Medium/gift.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Solution for https://www.codingame.com/ide/puzzle/the-gift
class Gift
attr_accessor :participants, :gift_price, :budgets_list

def initialize
@participants = gets.to_i
@gift_price = gets.to_i
@budgets_list = init_budgets_list
end

def init_budgets_list
Array.new(participants) { gets.to_i }.sort
end

def not_possible?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods should have one purpose. This one tells us if something is possible - true/false. That said, it will look something like:

budgets_list.inject(0, :+) < gift_price

budgets_list.inject(0, :+) < gift_price
end

def start
if not_possible?
puts 'IMPOSSIBLE'
return
end
money_to_pay = gift_price
budgets_list.each_with_index.each_with_object([]) do |(budget, index), result|
contributions_count = budgets_list.size - index
contribution = (money_to_pay / contributions_count).to_i
contribution = budget if budget < contribution
money_to_pay -= contribution
result << contribution
end
end
end

obj = Gift.new
puts obj.start