Question: Your task in this question is to write a function teamwork_climb(elevations, path, ponies) that simulates the movement of selfish and altruistic ponies and returns a

Your task in this question is to write a function teamwork_climb(elevations, path, ponies) that simulates the movement of selfish and altruistic ponies and returns a dictionary in the format described below. This function should have three arguments - elevations, path, and ponies as described in the previous question. Your function should return a dictionary where each key is an integer timestep (ie. 0, 1, 2...), and the values are a list of pony tuples at that timestep. Consistent with Q2, each ponies list should be sorted by id. That is, ponies with lower ids are at the front. Please also refer to Q2 for the definition of the final timestep. This task is similar to the task in the previous question, with the difference being that ponies either have the personality "s" or "a". Remember that at each time step, ponies try to move in order of ascending id (ie Pony 0, then Pony 1, then Pony 2 ... etc). Selfish pony behaviour Refer to Q2 Altruistic pony behaviour At each time step, an altruistic pony can give its energy to other ponies. Altruistic ponies will try to help other ponies move along the path before moving along the path itself. Let's look at a specific timestep t. For an altruistic pony P1 and pony of any type P2, if all of the following are true in P1's turn: P1 and P2 are in the same location P2 has a smaller id than P1 P2 is stuck at the location P1 can give enough energy to P2 so that P2 has enough energy to move to the next location After giving P2 the energy it requires, P1 still has non-negative energy Then the following two events will occur in order: In this timestep t, P1 gives P2 exactly enough energy to allow P2 to move to the next location In this timestep t, P1 moves to the next location if it still has sufficient energy to do so Notice that the pony P2 does not move at timestep t. Note that at a time step, an altruistic pony P1 can give its energy to multiple ponies. If there are multiple ponies that satisfy the five criteria above, the altruistic pony gives its energy to the pony with the lower id first. Also, if any of the five criteria for giving energy are not satisfied (that is, no ponies need help or P1 is not able to help), then the altruistic pony P1 behaves just like a selfish pony. Examples >>> result1 = teamwork_climb([[0, 2, 5], [0, 0, 10]], [(0, 0), (0, 1), (0, 2), (1, 2)], [(0, 's', [2, (0, 0)]), (1, 's', [6, (0, 0)]), (2, 'a', [14, (0, 0)])]) >>> result1 {0: [(0, 's', [2, (0, 0)]), (1, 's', [6, (0, 0)]), (2, 'a', [14, (0, 0)])], 1: [(0, 's', [0, (0, 1)]), (1, 's', [6, (0, 0)]), (2, 'a', [14, (0, 0)])], 2: [(0, 's', [0, (0, 1)]), (1, 's', [4, (0, 1)]), (2, 'a', [14, (0, 0)])], 3: [(0, 's', [0, (0, 1)]), (1, 's', [1, (0, 2)]), (2, 'a', [12, (0, 1)])], 4: [(0, 's', [3, (0, 1)]), (1, 's', [1, (0, 2)]), (2, 'a', [6, (0, 2)])], 5: [(0, 's', [5, (0, 2)]), (1, 's', [1, (0, 2)]), (2, 'a', [1, (0, 2)])], 6: [(0, 's', [0, (1, 2)]), (1, 's', [1, (0, 2)]), (2, 'a', [1, (0, 2)])]} >>> result2 = teamwork_climb([[0, 0], [10, 0]], [(0, 0), (1, 0), (1, 1)], [(0, 's', [0, (0, 0)]), (1, 'a', [10, (0, 0)])]) >>> result2 {0: [(0, 's', [0, (0, 0)]), (1, 'a', [10, (0, 0)])], 1: [(0, 's', [0, (0, 0)]), (1, 'a', [10, (0, 0)])], 2: [(0, 's', [10, (0, 0)]), (1, 'a', [0, (0, 0)])], 3: [(0, 's', [0, (1, 0)]), (1, 'a', [0, (0, 0)])], 4: [(0, 's', [10, (1, 1)]), (1, 'a', [0, (0, 0)])]} >>> result3 = teamwork_climb([[0], [15]], [(0, 0), (1, 0)], [(0, 's', [0, (0, 0)]), (1, 'a', [10, (0, 0)])]) >>> result3 {0: [(0, 's', [0, (0, 0)]), (1, 'a', [10, (0, 0)])], 1: [(0, 's', [0, (0, 0)]), (1, 'a', [10, (0, 0)])], 2: [(0, 's', [0, (0, 0)]), (1, 'a', [10, (0, 0)])]} >>> result4 = teamwork_climb([[0, 0], [10, 20]], [(0, 0), (1, 0), (1, 1)], [(0, 's', [10, (0, 0)]), (1, 'a', [30, (0, 0)])]) >>> result4 {0: [(0, 's', [10, (0, 0)]), (1, 'a', [30, (0, 0)])], 1: [(0, 's', [0, (1, 0)]), (1, 'a', [30, (0, 0)])], 2: [(0, 's', [0, (1, 0)]), (1, 'a', [20, (1, 0)])], 3: [(0, 's', [10, (1, 0)]), (1, 'a', [0, (1, 1)])], 4: [(0, 's', [0, (1, 1)]), (1, 'a', [0, (1, 1)])]}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!