Skip to content

Instantly share code, notes, and snippets.

@nelsonjchen
Forked from jroesch/pizza.hs
Created October 18, 2012 05:12
Show Gist options
  • Save nelsonjchen/3909963 to your computer and use it in GitHub Desktop.
Save nelsonjchen/3909963 to your computer and use it in GitHub Desktop.
ACM Micro-Challenge 1
require "test/unit"
# Write a function called numPizzas, that takes the number of people present,
# the number of slices in a pizza, and the time of day, and returns the
# number of pizzas to buy (as a whole integer).
#
# Spec:
# Signature: int numPizzas(int numPeople, int slicesPerPizza, int time)
# Time is an int on 24-hour clock (0-23), e.g. 8 = 8am, 14 = 2pm
# 11 = 11am 11pm = 23
# Between 11am - 11pm: 2 slices per person, else 1
#
# Example:
# numPizzas(7, 12, 10) ---> 7 people with 12 slices per pizza at 10am => 1 pizza
#
# Test cases:
# numPizzas(10, 10, 1) # => 1 (12am)
# numPizzas(10, 10, 1) # => 1 (1am)
# numPizzas(7, 12, 10) # => 1 (10am)
# numPizzas(20, 10, 11) # => 4 (11am)
# numPizzas(7, 12, 15) # => 2 (3pm)
# numPizzas(5, 10, 23) # => 1 (11pm)
def nPizza(p,s,t)((t<11||t>22?p:2*p).to_f/s).ceil end
class PizzaTest < Test::Unit::TestCase
def test_between_12am_and_11am # 1 slice per person
assert_equal 2, nPizza(10, 5, 0) # 12am
assert_equal 1, nPizza(10, 10, 1) # 1am
assert_equal 1, nPizza(7, 12, 10) # 10am
end
def test_between_11am_and_11pm # 2 slices per person
assert_equal 4, nPizza(20, 10, 11) # 11am
assert_equal 2, nPizza(7, 12, 15) # 3pm
end
def test_after_11pm # 1 slice per person
assert_equal 1, nPizza(5, 10, 23) # 11pm
assert_equal 2, nPizza(10, 5, 23) # 12am
assert_equal 1, nPizza(10, 10, 23) # 1am
assert_equal 1, nPizza(7, 12, 23) # 10am
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment