1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Oduvanchick [21]
2 years ago
7

find_cow(name, cows) Given a name and a Python list of Cow objects, return the Cow object with the specified name. If no such Co

w object can be found, return None.
Computers and Technology
1 answer:
nadya68 [22]2 years ago
4 0

Using the computational knowledge in python it is possible to write a code that Given a name and a Python list of Cow objects

<h3>Writting the code in python:</h3>

<em>def load_cows(filename):</em>

<em>      cow_dict = dict()</em>

<em>    f = open(filename, 'r')</em>

<em> </em>

<em>    for line in f:</em>

<em>        line_data = line.split(',')</em>

<em>        cow_dict[line_data[0]] = int(line_data[1])</em>

<em>    return cow_dict</em>

<em>def greedy_cow_transport(cows, limit=10):</em>

<em>       trips = []</em>

<em>    cowsCopy = cows.copy()</em>

<em>    sortedCows = sorted(cowsCopy.items(), key=lambda x: x[1], reverse = True)</em>

<em>    while sum(cowsCopy.values()) > 0:</em>

<em>        ship = []</em>

<em>        total = 0</em>

<em>        for cow, value in sortedCows:</em>

<em>            if cowsCopy[cow] != 0 and value + total <= limit:</em>

<em>                ship.append(cow)</em>

<em>                total += value</em>

<em>                cowsCopy[cow] = 0</em>

<em>        trips.append(ship)</em>

<em>    return trips</em>

<em>def brute_force_cow_transport(cows,limit=10):</em>

<em>     trips = []</em>

<em>     possibilities = []</em>

<em>    for i in power_list:</em>

<em>        ship = []</em>

<em>        for j in i:</em>

<em>            ship_weights = []</em>

<em>            for k in j:</em>

<em>                ship_weights.append(cows[k])</em>

<em>                #print(ship_weights)</em>

<em>            ship.append(sum(ship_weights))</em>

<em>            #print(ship)</em>

<em>        if all(d <= limit for d in ship):</em>

<em>            possibilities.append(i)</em>

<em>      pruned_possibilities = []</em>

<em>    for k in possibilities:</em>

<em>        if k not in pruned_possibilities:</em>

<em>            pruned_possibilities.append(k)</em>

<em>    # now find the minimum list length:</em>

<em>    min_list_len = min(map(len, pruned_possibilities))</em>

<em>    for l in pruned_possibilities:</em>

<em>        if len(l) == min_list_len:</em>

<em>            return l</em>

<em>       </em>

<em>def compare_cow_transport_algorithms():</em>

<em>       greedy_start = time.time()</em>

<em>    greedy_results = greedy_cow_transport(cows, limit = 10)</em>

<em>    greedy_end = time.time()</em>

<em>    print('Greedy Algorithm time:', greedy_end -greedy_start)</em>

<em>    brute_force_start = time.time()</em>

<em>    brute_force_results = brute_force_cow_transport(cows, limit = 10)</em>

<em>    brute_force_end = time.time()</em>

<em>    print('Brute force time:', brute_force_end - brute_force_start)</em>

<em>    print('Greedy Algorithm results:', greedy_results)</em>

<em>    print('Number of trips returned by Greedy Algorithm:', len(greedy_results))</em>

<em>    print('Brute Force Algorithm results:', brute_force_results)</em>

<em>    print('Number of trips returned by Brute Force Algorithm:', len(brute_force_results))</em>

   

See more about python at brainly.com/question/13437928

#SPJ1

You might be interested in
Shawn works at the local electrical company in Nampa, ID, which provides electricity to the entire city of Boise. This area is p
nexus9112 [7]

Answer:

D. Fault tolerance

Explanation:

  • Fault Tolerance basically refers to the feature or capability of a  system to keeps on working and performing its operations without interruption in case of a failure.
  • This failure can be the failure of one or more of its components.
  • This is beneficial as the system does not stop working or fail completely.
  • It provides copies of the same system or backup components which can be used when the existing system fails and prevents the loss of information and services.
  • Accessibility  refers to provide equal services for everyone who uses a system. So its not a correct option.
  • Moore’s law states that the computing speed and power will double every two years so its not a correct option as well.
  • Anti virus is a software to identify, prevent and remove viruses and malware so it is not a correct option as well.

3 0
3 years ago
A computer can read whatever you type into it.<br> A)True<br> B)False
motikmotik

Answer:

True

Explanation:

Everything typed into a computer is saved as data and logged.

3 0
3 years ago
Read 2 more answers
Is mayonnaise technically an instrument?
stich3 [128]
No it doesn’t count as an instrument
7 0
3 years ago
The art element line is a moving point.<br><br><br> True or False
sergeinik [125]
I’m pretty sure the answer is true
3 0
3 years ago
WILL MARK BRAINLIEST.....
slamgirl [31]

Answer:

Variables. A variable is a way of naming and storing a value for later use by the program, ... its type, and optionally, setting an initial value (initializing the variable). ... byte x; x = 0; x = x - 1; // x now contains 255 - rolls over in neg. direction

Explanation:

7 0
3 years ago
Other questions:
  • Electronic files created on a computer using programs such as word software are considered to be
    15·1 answer
  • How can you troubleshoot Internet access problems?
    11·1 answer
  • 4-One possible performance multiplication enhancement is to do a shift and add instead of an actual multiplication. Since 9 * 6,
    12·1 answer
  • True or False: When you share something on a friend’s Timeline, only that friend can see it.
    5·2 answers
  • You want to find the distance between two points.
    8·1 answer
  • Kos-huar-utb garls jonly​
    8·2 answers
  • A computer program that enables users to create and work with files and folders is called what?
    8·2 answers
  • Which option on the Format tab is used to modify particular portions of the chart?
    13·1 answer
  • Write a for loop that uses the loop control variable to take on the values 0 through 10.
    10·1 answer
  • Where are my files shortcut in documents folder.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!