danielharan (owner)

Revisions

gist: 54103 Download_button fork
public
Public Clone URL: git://gist.github.com/54103.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# We order a LOT of pizza at GiraffeSoft
# (there are 14 empty boxes on the table, and 3 more on the way)
# So, what's the cheapest way to buy them?
#
# in irb,
# include PizzaOptimizer
# compare([[9,11],[12,14],[14, 21]])
# 9" @ 11 => 0.270170428088094
# 12" @ 14 => 0.25788995408409
# 14" @ 21 => 0.331572798108115
module PizzaOptimizer
  def cost_per_square_inch(pizza_diameter, cost)
    cost.to_f / circle_area(pizza_diameter / 2)
  end
 
  def circle_area(radius)
    Math::PI * (radius ** 2)
  end
  
  def compare(pizzas)
    pizzas.each do |diameter, price|
      puts "#{diameter}\" @ #{price} => #{cost_per_square_inch(diameter, price)}"
    end
  end
end