Skip to content

[Gift task] Solution for gift task#9

Open
Slavunderkind wants to merge 3 commits into
masterfrom
gift_solution
Open

[Gift task] Solution for gift task#9
Slavunderkind wants to merge 3 commits into
masterfrom
gift_solution

Conversation

@Slavunderkind

Copy link
Copy Markdown
Owner

No description provided.

Comment thread Training/Medium/gift.rb Outdated
participants.times do
list << gets.to_i
end
list.sort

Copy link
Copy Markdown

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

Comment thread Training/Medium/gift.rb
list.sort
end

def not_possible?

Copy link
Copy Markdown

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

Comment thread Training/Medium/gift.rb Outdated
end

def start
return if not_possible?

Copy link
Copy Markdown

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

Comment thread Training/Medium/gift.rb Outdated
contribution = (money_to_pay / contributions_count).to_i
contribution = budget if budget < contribution
money_to_pay -= contribution
puts contribution

Copy link
Copy Markdown

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.

Comment thread Training/Medium/spoon.rb Outdated
end

def start
nodes.each do |node|

Copy link
Copy Markdown

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.

Comment thread 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))

Copy link
Copy Markdown

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.

Comment thread Training/Medium/spoon.rb Outdated
' -1 -1'
else
" #{result[0]} #{result[1]}"
end

Copy link
Copy Markdown

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant