Skip to content

Instantly share code, notes, and snippets.

@andrewberls
Created October 13, 2012 00:33
Show Gist options
  • Save andrewberls/3882585 to your computer and use it in GitHub Desktop.
Save andrewberls/3882585 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
# 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 numPizzas(numPeople, slicesPerPizza, time)
total_slices = (time.between?(11, 22)) ? (numPeople * 2) : numPeople
(total_slices / slicesPerPizza.to_f).ceil
end
class PizzaTest < Test::Unit::TestCase
def test_between_12am_and_11am # 1 slice per person
assert_equal 2, numPizzas(10, 5, 0) # 12am
assert_equal 1, numPizzas(10, 10, 1) # 1am
assert_equal 1, numPizzas(7, 12, 10) # 10am
end
def test_between_11am_and_11pm # 2 slices per person
assert_equal 4, numPizzas(20, 10, 11) # 11am
assert_equal 2, numPizzas(7, 12, 15) # 3pm
end
def test_after_11pm # 1 slice per person
assert_equal 1, numPizzas(5, 10, 23) # 11pm
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment