human
Capacity To Ship Packages Within D Days | Function to check if the weights can be delivered in D days or not.
Stores the count of days required to ship all the weights if the maximum capacity is mx.
Traverse all the weights.
If total weight is more than the maximum capacity.
If days are more than D , then return false.
Return true for the days < D.
Function to find the least weight capacity of a boat to ship all the weights within D days.
Stores the total weights to be shipped.
Find the sum of weights.
Stores the maximum weight in the array that has to be shipped.
Store the ending value for the search space.
Store the required result.
Perform binary search.
Store the middle value.
If mid can be shipped , then update the result and end value of the search space.
Search for minimum value in the right part.
Print the result.
Driver Code"
How can the above be solved in Python?
 ... Give me a code to capacity To Ship Packages Within D Days and the other steps required?
assistant
#Here is code to capacity To Ship Packages Within D Days and the other steps required.
 def isValid ( weight , n , D , mx ) :
	st = 1
	sum = 0
	for i in range ( n ) :
		sum += weight [ i ]
		if ( sum > mx ) :
			st += 1
			sum = weight [ i ]
		if ( st > D ) :
			return False
	return True
def shipWithinDays ( weight , D , n ) :
	sum = 0
	for i in range ( n ) :
		sum += weight [ i ]
	s = weight [ 0 ]
	for i in range ( 1 , n ) :
		s = max ( s , weight [ i ] )
	e = sum
	res = - 1
	while ( s <= e ) :
		mid = s + ( e - s ) // 2
		if ( isValid ( weight , n , D , mid ) ) :
			res = mid
			e = mid - 1
		else :
			s = mid + 1
	print ( res )
if __name__ == ' _ _ main _ _ ' :
	weight = [ 9 , 8 , 10 ]
	D = 3
	N = len ( weight )
	shipWithinDays ( weight , D , N )