Posts

Showing posts with the label codingbat

make_chocolate

Image
  make_chocolate We want to make a package of  goal  kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done. make_chocolate(4, 1, 9) → 4 make_chocolate(4, 1, 10) → -1 make_chocolate(4, 1, 7) → 2 solution: Python code:  def make_chocolate(small, big, goal):   a=goal%5   if a<=small and (goal/5)<=big:     return a   b=goal-(big *5)   if b<=small and b>=0:     return b   return -1