Skip to content

Instantly share code, notes, and snippets.

@rupalbarman
Created March 27, 2017 14:03
Show Gist options
  • Select an option

  • Save rupalbarman/83dca02f252e314899b90b85b277d383 to your computer and use it in GitHub Desktop.

Select an option

Save rupalbarman/83dca02f252e314899b90b85b277d383 to your computer and use it in GitHub Desktop.
Simple Bin Packing Algorithm code which uses Next-fit technique
# bin packing algo (estimation)
# since it is Np- Hard
# for better methods (first fit and best fit) use self balancing trees
# this one is Next fit
def bins_needed(item_weights, bin_capacity):
remain= bin_capacity
bins=0
for i in item_weights:
if i> bin_capacity:
bins+=1
remain= bin_capacity
else:
bin_capacity-= i
return bins
wt= [2, 5, 4, 7, 1, 3, 8]
cap= 10
print(bins_needed(wt, cap))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment