This is a tip to the recursion problem from C-thread
Given are packages, each with a given weight and a number of trucks with a max load capacity. The packages should be loaded on the trucks.
The problem should be solved recursivelly.
E.g we have this list of packages
packages = [5, 3, 8, 2, 1, 2, 3, 5, 2]
i.e.
1. package with weight 5
2. package with weight 3
..etc
and 3 trucks with the same max capacity=10
For Loading one truck I use recursive procedure:
1. I try to load first package from the list
2. and then I try to load the rest of packages.
For loading all packages on the 3 trucks:
1. load first truck
2. then load second truck
3. at end load last truck
Here is the program trucks.py
The output demonstrates how it works
Given are packages, each with a given weight and a number of trucks with a max load capacity. The packages should be loaded on the trucks.
The problem should be solved recursivelly.
E.g we have this list of packages
packages = [5, 3, 8, 2, 1, 2, 3, 5, 2]
i.e.
1. package with weight 5
2. package with weight 3
..etc
and 3 trucks with the same max capacity=10
For Loading one truck I use recursive procedure:
1. I try to load first package from the list
2. and then I try to load the rest of packages.
For loading all packages on the 3 trucks:
1. load first truck
2. then load second truck
3. at end load last truck
Here is the program trucks.py
Code:
def truck(load, capacity, packages):
"""
Parameters:
load = list of packages loaaded on the truck
capacity = maximal possible weight of loading
packages = plist of packages available
"""
print "Calling: truck(%s, %s, %s)" % (load, capacity, packages)
# compute load weight
total_load=total(load)
if packages != [] and total_load != capacity :
# try to load first package to truck
load.append(packages[0])
# compute load weight
total_load=total(load)
if total_load > capacity:
# remove last package from truck
load = load[:-1]
# try with the next package
load=truck(load, capacity, packages[1:])
# return list of packages loaded on the truck
return load
def total(packages):
sum = 0
for package in packages:
sum += package
# return value
return sum
def list_difference(list, sublist):
difference_list=list
for element in sublist:
if element in list:
difference_list.remove(element)
return difference_list
def main():
capacity = 10
packages = [5, 3, 8, 2, 1, 2, 3, 5, 2]
# 1.Truck
print "Original packages = %s\n" % packages
print "Loading packages on 1.Truck:"
load=truck([], capacity, packages)
print "1.Truck loaded with packages %s => total weight=%d\n"\
% (load, total(load))
# 2.Truck
packages = list_difference(packages, load)
print "Remaining packages = %s" % packages
print "Now loading remaining packages on 2.Truck:"
load=truck([], capacity, packages)
print "2.Truck loaded with packages %s => total weight=%d\n"\
% (load, total(load))
# 3.Truck
packages = list_difference(packages, load)
print "Remaining packages = %s" % packages
print "Now loading remaining packages on 3.Truck:" % packages
load=truck([], capacity, packages)
print "3.Truck loaded with packages %s => total weight=%d\n"\
% (load, total(load))
# Packages remaining at end
packages = list_difference(packages, load)
print "Remaining packages = %s" % packages
if __name__ == "__main__":
main()
The output demonstrates how it works
Code:
D:\>trucks.py
Original packages = [5, 3, 8, 2, 1, 2, 3, 5, 2]
Loading packages on 1.Truck:
Calling: truck([], 10, [5, 3, 8, 2, 1, 2, 3, 5, 2])
Calling: truck([5], 10, [3, 8, 2, 1, 2, 3, 5, 2])
Calling: truck([5, 3], 10, [8, 2, 1, 2, 3, 5, 2])
Calling: truck([5, 3], 10, [2, 1, 2, 3, 5, 2])
Calling: truck([5, 3, 2], 10, [1, 2, 3, 5, 2])
1.Truck loaded with packages [5, 3, 2] => total weight=10
Remaining packages = [8, 1, 2, 3, 5, 2]
Now loading remaining packages on 2.Truck:
Calling: truck([], 10, [8, 1, 2, 3, 5, 2])
Calling: truck([8], 10, [1, 2, 3, 5, 2])
Calling: truck([8, 1], 10, [2, 3, 5, 2])
Calling: truck([8, 1], 10, [3, 5, 2])
Calling: truck([8, 1], 10, [5, 2])
Calling: truck([8, 1], 10, [2])
Calling: truck([8, 1], 10, [])
2.Truck loaded with packages [8, 1] => total weight=9
Remaining packages = [2, 3, 5, 2]
Now loading remaining packages on 3.Truck:
Calling: truck([], 10, [2, 3, 5, 2])
Calling: truck([2], 10, [3, 5, 2])
Calling: truck([2, 3], 10, [5, 2])
Calling: truck([2, 3, 5], 10, [2])
3.Truck loaded with packages [2, 3, 5] => total weight=10
Remaining packages = [2]