Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem A2, Greedy Cow Transport One way of transporting cows is to always pick the heaviest cow that will fit onto the spaceship first. This

Problem A2, Greedy Cow Transport

One way of transporting cows is to always pick the heaviest cow that will fit onto the spaceship first. This is an example of a greedy algorithm. So if theres only 2 tons of free space on your spaceship, with one cow thats 3 tons and another thats 1 ton, the 1 ton cow will still get put onto the spaceship.

Implement a greedy algorithm for transporting the cows back across space in greedy_cow_transport. The result should be a list of lists, where each inner list represents a trip and contains the names of cows taken on that trip.

Note:

Make sure not to mutate the dictionary of cows that is passed in!

Assumptions:

  • The order of the list of trips does not matter. That is, [[1,2],[3,4]] and [[3,4],[1,2]] are considered equivalent lists of trips.

  • All the cows are between 0 and 10 tons in weight

  • All the cows have unique names

  • If multiple cows weigh the same amount, break ties arbitrarily

  • The spaceship has a cargo weight limit (in tons), which is passed into the function as a

    parameter

In [ ]: def greedy_cow_transport(cows,limit=10): """

 Uses a greedy heuristic to determine an allocation of cows that attempts to minimize the number of spaceship trips needed to transport all the cows. The returned allocation of cows may or may not be optimal. The greedy heuristic should follow the following method: 
 1. As long as the current trip can fit another cow, add the largest cow that will f to the trip 
 2. Once the trip is full, begin a new trip to transport the remaining cows Does not mutate the given dictionary of cows. 
 Parameters: cows - a dictionary of name (string), weight (int) pairs limit - weight limit of the spaceship (an int) 

Returns: A list of lists, with each inner list containing the names of cows transported on a particular trip and the overall list containing all the trips """ # TODO: Your code here pass

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

Can workers be trained in ethics? How? Defend your answer.

Answered: 1 week ago