Skip to content

Instantly share code, notes, and snippets.

@jdiscar
Last active August 29, 2015 13:56
Show Gist options
  • Save jdiscar/9144525 to your computer and use it in GitHub Desktop.
Save jdiscar/9144525 to your computer and use it in GitHub Desktop.
Camels and Carrots Problem (Python)
import math
# Question:
# Steve has a carrot farm and a camel. He wants to transport 300 carrots to the market,
# which is located 100 miles away. The camel will carry the carrots. The camel can carry
# a maximum of 100 carrots at a time, and it needs to eat one carrot for every mile it
# travels. What is the maximum number of carrots can the camel deliver to the market?
# Answer:
# We essentially want to move all the carrots we can carry to midpoints until we reach
# our destination. Here's the recursive calculation in Python:
def getMaxCarrots(totalCarrots,totalDistance,carryLimit,carrotsConsumedPerMile):
# Recursive Base Case: You can carry all the carrots, so try to move all the carrots
if totalCarrots <= carryLimit:
carrotsAtDestination = totalCarrots - (totalDistance * carrotsConsumedPerMile)
if carrotsAtDestination >= 0.0:
print "Move Everything %i miles over the final trip." % totalDistance
return carrotsAtDestination
else:
return 0.0
# How many round trips we need to consume all the carrots
# Subtract one from it
maxTripsMinusOne = math.ceil(totalCarrots/carryLimit) - 1
# Total Number of one way trips you need to travel to transport all the carrots,
# assuming you carry the maximum number of carrots per trip. You don't need to
# return to the farm for the last trip, so add one instead of two.
numTrips = 2 * maxTripsMinusOne + 1
# Carrot Cost Per Merged Mile. 'Merged mile' because we want to take [numTrips] trips,
# which includes the trip back
carrotsConsumedPerMergedMile = numTrips * carrotsConsumedPerMile
# Remaining carrots, after eating, if we make one less round trip.
# We'll leave behind any carrots that will require more energy
# than they're worth to fetch.
remainingCarrots = carryLimit * maxTripsMinusOne
# The totalDistance you travel before you require one less round trip
# This is derived from the more obvious:
# remainingCarrots = totalCarrots - (traveled * carrotsConsumedPerMergedMile)
traveled = (totalCarrots - remainingCarrots) / carrotsConsumedPerMergedMile
# If we can travel greater or equal than the remaining totalDistance,
# then just bring all the carrots right to the destination
if( traveled >= totalDistance ):
return totalCarrots - (totalDistance * carrotsConsumedPerMergedMile)
# Print out our current status
print ("Move Everything %i miles forward over %i trips, still have %i carrots"
"and %i miles to go.") % (traveled, numTrips, remainingCarrots, totalDistance-traveled)
# Recalculate recursively from our new starting point
return getMaxCarrots(remainingCarrots, totalDistance-traveled, carryLimit, carrotsConsumedPerMile)
print "Answer: "+str(getMaxCarrots(300,100,100,1))+" carrots can be delivered to the market"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment