Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Greedy Cow Transport
#1
A colony of Aucks (super-intelligent alien bioengineers) has landed on Earth and has created new species of farm animals! The Aucks are performing their experiments on Earth, and plan on transporting the mutant animals back to their home planet of Aurock. In this problem set, you will implement algorithms to figure out how the aliens should shuttle their experimental animals back across space.
The aliens have succeeded in breeding cows that jump over the moon! Now they want to take home their mutant cows. The aliens want to take all chosen cows back, but their spaceship has a weight limit and they want to minimize the number of trips they have to take across the universe. Somehow, the aliens have developed breeding technology to make cows with only integer weights.


def load_cows(filename):
    cow_dict = dict()
    f = open(filename, 'r')
    for line in f:
        line_data = line.split(',')
        cow_dict[line_data[0]] = int(line_data[1])
    return cow_dict

def greedy_cow_transport(cows,limit=10):
    lista = []
    totalWeight = 0
    scows = sorted(cows)
    while totalWeight <= limit:
        for key, value in enumerate(scows):
            totalWeight += value
            lista.append(cow)
        return lista 

cows = load_cows("ps1_cow_data.txt")
limit=100
print(cows)

print(greedy_cow_transport(cows, limit))
print(brute_force_cow_transport(cows, limit))
error:
Error:
Traceback (most recent call last): File "C:\Python36\kodovi\6.00.2x\ps1.py", line 79, in <module> cows = load_cows("ps1_cow_data.txt") File "C:\Python36\kodovi\6.00.2x\ps1.py", line 16, in load_cows cow_dict[line_data[0]] = int(line_data[1]) ValueError: invalid literal for int() with base 10: '7Betsy'
this is how ps1_cow_data.txt looks like:
Maggie,3
Herman,7Betsy,9
Oreo,6
Moo Moo,3
Milkshake,2
Millie,5
Lola,2Florence,2Henrietta,9

I don't understand this error message.
I assume that function argument limit=10 refers to the weight that spaceship can take in one flight and 100 is how much weight they want at all.
Reply


Messages In This Thread
Greedy Cow Transport - by Truman - Apr-08-2019, 10:13 PM
RE: Greedy Cow Transport - by Gribouillis - Apr-08-2019, 10:22 PM
RE: Greedy Cow Transport - by ichabod801 - Apr-08-2019, 10:30 PM
RE: Greedy Cow Transport - by perfringo - Apr-09-2019, 05:19 AM
RE: Greedy Cow Transport - by Skaperen - Apr-09-2019, 05:49 AM
RE: Greedy Cow Transport - by Truman - Apr-09-2019, 10:29 PM
RE: Greedy Cow Transport - by ichabod801 - Apr-09-2019, 10:45 PM
RE: Greedy Cow Transport - by Truman - Apr-09-2019, 10:57 PM
RE: Greedy Cow Transport - by perfringo - Apr-10-2019, 03:28 AM
RE: Greedy Cow Transport - by Truman - Apr-10-2019, 09:33 PM
RE: Greedy Cow Transport - by perfringo - Apr-11-2019, 04:03 AM
RE: Greedy Cow Transport - by perfringo - Apr-11-2019, 11:38 AM
RE: Greedy Cow Transport - by Truman - Apr-11-2019, 11:06 PM
RE: Greedy Cow Transport - by perfringo - Apr-12-2019, 04:40 AM
RE: Greedy Cow Transport - by Truman - Apr-14-2019, 11:18 PM
RE: Greedy Cow Transport - by perfringo - Apr-15-2019, 07:14 AM
RE: Greedy Cow Transport - by Truman - Apr-15-2019, 08:42 PM
RE: Greedy Cow Transport - by perfringo - Apr-18-2019, 10:47 AM
RE: Greedy Cow Transport - by Truman - Apr-18-2019, 02:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I correct multiplication error? Greedy algorithm help student8 1 3,943 Oct-02-2017, 03:00 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020