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
    • 435
    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:
                    total += value
                    cows[cow] = 0
        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:
     ['Herman', 'Maggie'],
     ['Oreo', 'Moo Moo'],
     ['Millie', 'Milkshake', 'Lola'],
    How to display this way? Is this going to be counted in greedy algorithm?

 0 Answer(s)

Sign In

Sign up using

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: