Question: Please write in Python 3, Thanks! Suppose that you have a triangular arrangment of integer values such as: 9 1 -3 5 -2. 4 You



Please write in Python 3, Thanks!
Suppose that you have a triangular arrangment of integer values such as: 9 1 -3 5 -2. 4 You can move from a number down one layer to a number immediately to the left or right (for example, you can move from 1 to 5 or -2, but not to 4). Each number that you move to incurs a cost equal to the number. You want to find the lowest cost of a path from the top of the triangle to the bottom of the triangle. In the triangle shown above, there are 4 paths having costs: . 9+1+5 = 15 9+1+(-2) = 8 9+(-3) + (-2) = 4 .9+(-3) + 4 = 10 so the lowest cost path has a cost of 4. To represent the triangular arrangment of values we will use a list of n sublists similar to that returned by the to_triangular_grid function from the previous question. For example, the triangle of numbers shown above is represented by the list: t = [[9, -3, 4], [1, -2], [5]] The top-most number of the triangle is given by t[0][0] . Write the function triangle_cost_impl(t, i, j) that recursively computes the lowest cost path to the bottom of the triangle starting at t[i][j] . Note that there is no upper limit on the height of the triangle. You should not call the function triangle_cost_impl directly, instead call the function triangle_cost(t) which returns the value of triangle_cost_impl(t, 0, @) . See the assignment module a3 where triangle_cost(t) is already given to you. A few more examples of triangular arrangment of values and their lowest cost paths are given below: 3 t = [[3]] cost = a3. triangle_cost(t) # should be 3 (path 3) 1 3 2 t = [[1, 2], [3]] cost = a3. triangle_cost(t) # should be 3 (path 1, 2) 7 4 0 -5 -3 6 3 -2 -1 5 t = [[7, 8, 6, 5], [4, -3, -1], [-5, -2], [3]] cost = a3.triangle_cost(t) # should be 2 (path 7, 0, 6, -1) def triangle_cost(t): return triangle_cost_impl(t, 0, 0) def triangle_cost_impl(t, row, col)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
