Question: Oh no ! Your pet sloth has managed to get itself lost in some hilly grasslands. It needs to make its way across this area
Oh no Your pet sloth has managed to get itself lost in some hilly grasslands. It needs to make its way across this area to get back to its home in the forest. Since sloths aren't so great at crawling on hills, it needs to conserve as much energy as possible. Fortunately, your sloth is wellversed in computer science and also has access to GPS data on the elevation of the area.
We will model the grasslands as a grid of elevations, stored in a rectangular D list of int values. Your sloth starts somewhere on the western edge of this grid, and its objective is to travel to the eastern edge where the forest lies. The sloth moves west to east one column at a time. The sloth wants to make constant progress towards the eastern edge, so it always chooses to move northeast, east, or southeast. It picks the direction that minimizes its energy usage, which is calculated like this:
If the sloth is moving from a lower elevation to a higher elevation its energy usage is
If the sloth is moving from a higher elevation to a lower elevation its energy usage is just after all, it has gravity on its side
If the sloth is moving from one elevation to the same elevation, its energy usage is
If two or more directions have the same energy usage, it chooses the northernmost one ie northeast is prioritized over east, and east is prioritized over southeast Note that if the sloth is on the northern or southern edge of the grasslands, either the northeast or southeast direction is no longer an option.
For example, suppose the grasslands elevations are given by the list below:
table
The sloth always starts from the western edge ie column of the list Suppose the sloth starts from row column This is the lower left corner of the grid, where the elevation is
From row column it must choose to go elevation to energy usage: or elevation to energy usage: NE has the lowest energy usage, so the sloth moves northeast to row column
From row column it must choose to go NE elevation to energy usage: elevation to energy usage: or SE elevation to energy usage: E has the lowest energy usage, so it moves east to row column
From row column it must choose to go NE elevation to energy usage: elevation to energy usage: or SE elevation to energy usage: E has the lowest energy usage, so it moves east to row column
From row column it must choose to go NE elevation to energy usage: elevation to energy usage: or elevation to energy usage: and are tied for lowest energy usage. The sloth chooses the northernmost of the tied directions, so it moves northeast to row column
The sloth reaches the forest as soon as it gets to any point in the last column, so we're now done. Yay!!
Create a Python file named lostsloth.py Within that file, write a function findpathdata startingrow The parameter data is a D list of int elevation values; the parameter startingrow is the index of the sloth's starting row on the western edge. Assume the list is rectangular and nonempty, and the starting row is a valid row in the list. The function returns a list containing the indices of the rows of the sloth's path as it moves from west to east, according to the previously described algorithm.
For example, if data is a D int list containing the data above, then calling findpathdata should return the list
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
