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
I know that this will be taken down for not being a real question but is anyone else not able to search for anything? I can type
Schach [20]

Answer:

Sameee

Explanation:

6 0
3 years ago
When two or more people come together to communicate, each person brings their own ___________________.
dimulka [17.4K]

When two people come together,  each with a clear definition of their  identities, the potential can be astounding.  A successful communication is composed of two  individuals - each with a clearly defined sense  of her or his own identity.  

6 0
3 years ago
Write an expression that will cause the following code to print "Equal" if the value of sensorReading is "close enough" to targe
Likurg_2 [28]

Answer:

The program to this question as follows:

Program:

targetValue = 0.3333 #defining variable targetValue and assign value

sensorReading = 0.0 #defining variable sensorReading and assign value

sensorReading = 1.0/3.0 #calculate value in sensorReading variable

Val=sensorReading - targetValue

#calculate the difference and store in Val variable

if (Val < 0.0001): #use of if block to check condition

  print ("Equal")  #print value

else:  #else block

  print ("Not equal") #print value

Output:

Equal

Explanation:

In the above Python program code, there are two variables "targetValue and sensorReading" is defined, in which targetValue store a value, that is "0.3333", and sensorReading holds a value, that is "0.0".

  • In the next step, the "Val" variable is defined, that calculate the difference between both variable, that conditional statement is used.
  • In if block, if the value is less then "0.0001", it will print value "Equal", otherwise, it will go to the else block, that will print "Not equal".
3 0
3 years ago
Read 2 more answers
Is mmorpg an example of virtual community
Inga [223]
Yes I would say it is.
4 0
3 years ago
Read 2 more answers
4.which pre-defined feature is known as functions in Excel?<br> a.formula<br> b.property<br> c. data
lesya692 [45]

Answer:

I think formula?

Explanation:

8 0
2 years ago
Other questions:
  • Explain why living things store energy in lipids instead of in carbohydrates
    5·1 answer
  • What are the basic tools for coding HTML manually?
    10·2 answers
  • For your biology class, you have taken a number of measurements for a plant growth experiment. You wish to create a chart that s
    10·1 answer
  • When installing a device driver, start the computer in
    6·1 answer
  • write a c++ program that writes weather data from a file wx_data.txt to a file wx_summary for everyday of the year. also add min
    15·1 answer
  • Each web site contains a(n) ____ page, which is the first document users see when they access the site.
    9·1 answer
  • The 10 and 2 o'clock hand position on the steering wheel is no longer recommended because _____.
    13·1 answer
  • Melissa is writing a class called Cell. Which method has she set up to return a double?
    7·1 answer
  • Discuss the ways you can perform to prevent your computer/device and its data/contents from being stolen. Define two-facto authe
    7·1 answer
  • write an assembly program that uses the output compare function of a timer to toggle an led every second
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!