Skip to content

Instantly share code, notes, and snippets.

@Roasbeef
Forked from nelsonjchen/pizza.rb
Created October 18, 2012 19:11
Show Gist options
  • Save Roasbeef/3914161 to your computer and use it in GitHub Desktop.
Save Roasbeef/3914161 to your computer and use it in GitHub Desktop.
ACM Micro-Challenge 1
from math import ceil
# 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)
n_pizza = lambda p, s, t: ceil((p*2 if t in range(11,23) else p)/float(s))
def test_n_pizza():
assert n_pizza(10, 10, 1) == 1
assert n_pizza(10, 10 ,1) == 1
assert n_pizza(7, 12, 10) == 1
assert n_pizza(20, 10, 11) == 4
assert n_pizza(7, 12, 15) == 2
assert n_pizza(5, 10, 23) == 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment