Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Correct or Get The Mistake in This Python Code?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 10
    Answer it
    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: