def greedy_cow_transport(cows,limit=10):
train = []
while sum(cows.values()) > 0:
cart = []
total = 0
for cow, value in sorted(cows.items(), key=lambda x:x[1], reverse=True):
if cows[cow] != 0 and value + total <= limit:
cart.append(cow)
total += value
cows[cow] = 0
train.append(cart)
return train
The code above gives the output:
[['Betsy', 'Henrietta', 'Herman', 'Oreo', 'Millie', 'Maggie', 'Moo Moo', 'Milkshake', 'Lola', 'Florence']]
I want output to be displayed as shown below:
[['Betsy'],
['Henrietta'],
['Herman', 'Maggie'],
['Oreo', 'Moo Moo'],
['Millie', 'Milkshake', 'Lola'],
['Florence']]
How to display this way? Is this going to be counted in greedy algorithm?
0 Answer(s)