-
Notifications
You must be signed in to change notification settings - Fork 0
[Gift task] Solution for gift task #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Training/Medium/gift.rb
Outdated
| participants.times do | ||
| list << gets.to_i | ||
| end | ||
| list.sort |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.new(participants) do
...
end.sort| list.sort | ||
| end | ||
|
|
||
| def not_possible? |
There was a problem hiding this comment.
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
Training/Medium/gift.rb
Outdated
| end | ||
|
|
||
| def start | ||
| return if not_possible? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing not_possible? method will require some refactoring here as well:
if not_possible?
puts 'IMPOSSIBLE'
return
end
Training/Medium/gift.rb
Outdated
| contribution = (money_to_pay / contributions_count).to_i | ||
| contribution = budget if budget < contribution | ||
| money_to_pay -= contribution | ||
| puts contribution |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to collect the results in an array and print it in a print method or in the bottom of the file when you start the script.
Training/Medium/spoon.rb
Outdated
| end | ||
|
|
||
| def start | ||
| nodes.each do |node| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use map and collect the result, so that it is returned from calling start and you'll avoid printing in the middle of an object's method.
Training/Medium/spoon.rb
Outdated
| nodes.each do |node| | ||
| result = "#{node[0]} #{node[1]}" | ||
| result += add_coordinates(find_right_node(node)) | ||
| result += add_coordinates(find_bottom_node(node)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use << operator where suitable, because it's faster. Doesn't create extra string objects.
Training/Medium/spoon.rb
Outdated
| ' -1 -1' | ||
| else | ||
| " #{result[0]} #{result[1]}" | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may have the same result with:
result ? " #{result[0]} #{result[1]}" : ' -1 -1'Also this methods doesn't actually add any coordinates, it just returns them -> def coordinates(result)
Put print action out of the class and use each_with_object to fill array with contributions
f55e9c8 to
7563008
Compare
No description provided.