Question
# No lambda Your task in this question is to write a function selfish_climb(elevations, path, ponies) that simulates the movement of selfish ponies and returns
# No lambda
Your task in this question is to write a function selfish_climb(elevations, path, ponies) that simulates the movement of selfish 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. In this question, all ponies in the ponies list have the personality "s". 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. The final timestep is defined below. As in the previous question, each ponies list should be sorted by id. That is, ponies with lower ids are at the front. Selfish Pony Behaviour The "s" (selfish) ponies only care about themselves. Each selfish pony travels individually along the path, completely disregarding other ponies also travelling on the path. Pony energy follows these rules: Ponies use energy when travelling to higher elevations. Ponies gain energy when they move to lower elevations. Energy usage is the elevation difference between the source location and the destination. A pony cannot have negative energy. For example, when an "s" pony with an energy of 52 moves from elevation 13 to 30, its energy is reduced by 17, leaving it with energy = 35. Similarly, when an "s" pony with an energy of 54 moves from elevation 30 to 13, its energy is increased by 17, leaving it with an energy of 71. A selfish pony with 10 energy can not move from elevation 20 to 31, because that would lead to negative energy - so it is stuck at its current location. Simulate Movement In this question, you are asked to simulate the movement of selfish ponies along the path. Movement follows these rules: At a given time t, the pony with id < t can move, where t is a valid integer less than or equal to the value of the final timestep. For example, at t=0, all ponies with id < 0 consider a move, and at t=1, all ponies with id < 1 consider a move. A pony may or may not move after a move is considered, depending on (1) the energy difference and (2) if the pony has reached the destination. At each timestep, ponies move in order of ascending id, ie pony 0 would move before pony 1. Note that you can assume all ponies have unique ids, and the ids of ponies in the ponies list are continuous integers in ascending order. The smallest id is 0, and the largest id is len(ponies) - 1. Final Timestep There must be enough timesteps so that every pony considers at least one move. The final timestep is the earliest timestep during which both of the following conditions are met: each pony has considered at least one move each pony either has insufficient energy to move or has arrived at the final destination. Hints Say the final timestep is t_final, then if your program continues to run after t = t_final, no pony will change their status. Examples >>> result1 = selfish_climb([[0, 32, 45], [2, 5, 19], [7, 6, 25]], [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2)], [(0, 's', [7, (0, 0)]), (1, 's', [2, (0, 0)]), (2, 's', [25, (0, 0)])]) >>> result1 {0: [(0, 's', [7, (0, 0)]), (1, 's', [2, (0, 0)]), (2, 's', [25, (0, 0)])], 1: [(0, 's', [5, (1, 0)]), (1, 's', [2, (0, 0)]), (2, 's', [25, (0, 0)])], 2: [(0, 's', [0, (2, 0)]), (1, 's', [0, (1, 0)]), (2, 's', [25, (0, 0)])], 3: [(0, 's', [1, (2, 1)]), (1, 's', [0, (1, 0)]), (2, 's', [23, (1, 0)])], 4: [(0, 's', [1, (2, 1)]), (1, 's', [0, (1, 0)]), (2, 's', [18, (2, 0)])], 5: [(0, 's', [1, (2, 1)]), (1, 's', [0, (1, 0)]), (2, 's', [19, (2, 1)])], 6: [(0, 's', [1, (2, 1)]), (1, 's', [0, (1, 0)]), (2, 's', [0, (2, 2)])]} >>> result2 = selfish_climb([[0, 10, 20], [0, 0, 20], [0, 0, 0]], [(0, 0), (0, 1), (0, 2), (1, 2), (1, 3)], [(0, 's', [0, (0, 0)]), (1, 's', [0, (0, 0)])]) >>> result2 {0: [(0, 's', [0, (0, 0)]), (1, 's', [0, (0, 0)])], 1: [(0, 's', [0, (0, 0)]), (1, 's', [0, (0, 0)])], 2: [(0, 's', [0, (0, 0)]), (1, 's', [0, (0, 0)])]} >>> result3 = selfish_climb([[0, 0], [0, 0]], [(0, 0), (1, 0), (1, 1)], [(0, 's', [0, (0, 0)])]) >>> result3 {0: [(0, 's', [0, (0, 0)])], 1: [(0, 's', [0, (1, 0)])], 2: [(0, 's', [0, (1, 1)])]} >>> result4 = selfish_climb([[100, 90, 80, 0], [30, 20, 40, 100], [31, 45, 0, 400]], [(0, 0), (0, 1), (0, 2), (1, 2), (1, 3), (2, 3)], [(0, 's', [0, (0, 0)]), (1, 's', [10, (0, 0)])]) >>> result4 {0: [(0, 's', [0, (0, 0)]), (1, 's', [10, (0, 0)])], 1: [(0, 's', [10, (0, 1)]), (1, 's', [10, (0, 0)])], 2: [(0, 's', [20, (0, 2)]), (1, 's', [20, (0, 1)])], 3: [(0, 's', [60, (1, 2)]), (1, 's', [30, (0, 2)])], 4: [(0, 's', [0, (1, 3)]), (1, 's', [70, (1, 2)])], 5: [(0, 's', [0, (1, 3)]), (1, 's', [10, (1, 3)])]} Hints Lists are mutable. Make sure you understand what happens in the following example: lst = [1, 2] container = (1, lst) lst_ptr = container[1] lst_ptr[0] = 10 print(container) To create a copy of a list, you can use .copy() or .deepcopy(). You can check them out here (we helped you import the copy module already, but you are not required to use it).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started