Question

PYTHON 3

PLEASE FOLLOW INSTRUCTIONS

#COMMENT STEPS :)1 triangLe-L 2 10], 3 C15,19], 4 [65,12,78], 5 [11,99,54,29 6 7 8 def maxPath(R, C, triangle):

Problem

Given a triangle of integers, we want to find the path that has the largest sum going from the top to the bottom of the triangle. The way we go down the triangle is by moving down level by level, at each level having the choice to either go straight down to the integer directly below, or the integer below and to the right.

Consider the following triangle:

 
 

10

 

25 13

 

19 78 65

 

17 22 54 89

We start moving down the triangle from the top, with the node 10, where we have two options to choose from:

1519403025471_d6a27d4f220a81ed53696c0875

For the triangle given above, the maximum path from the top to the bottom is shown below:

1519403297397_aba5a84f33c9328e86001de166

Notice how the maximum path isn't necessarily always given by picking the element with the highest number at each step (see how we pick 15 over 19 when we are at 10 and trying to go from the first level to the second level).

Hint: consider solving this problem recursively:

Think about the base case. What's the simplest form of this problem that we can solve?

Think about how to articulate the two choices you have at each step

Think about how you're going to pick the best choice at each step

Compute this by completing the function maxPath(), which takes in a row R and a column C and a 2d-array triangle, and returns the maximum path from (R, C) to the bottom of the triangle. The top row in the triangle will have index 0, and the leftmost column will also have index 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program :

# function for finding maximum sum
def maxPath(R, C, triangle):

# loop for bottom to top calculation of largest sum

for i in range(R-1, -1, -1):

for j in range(i+1):

# for each value, check both values just below it and below right to it and add the maximum of them to it

if (triangle[i+1][j] > triangle[i+1][j+1]):

triangle[i][j] += triangle[i+1][j]

else:

triangle[i][j] += triangle[i+1][j+1]

# return the topmost value in triangle which stores the maximum sum result

return triangle[0][0]

triangle = [
[10],
[15, 19],
[65, 12, 78],
[11, 99, 54, 29]
]

print(maxPath(3, 3, triangle)) # calling the function maxPath and printing the value returned by it

Output :

En] sourav@sourav-HP-Pavilion: sourav@sourav-HP-Pavilion- vim maxPath.py sourav@sourav-HP-Pavilion-$ python3 maxPath.py 189 s

Add a comment
Know the answer?
Add Answer to:
PYTHON 3 PLEASE FOLLOW INSTRUCTIONS #COMMENT STEPS :) Problem Given a triangle of integers, we want...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with Pascal’s Triangle: Paths and Binary Strings Suppose you want to create a path between...

    Help with Pascal’s Triangle: Paths and Binary Strings Suppose you want to create a path between each number on Pascal's Triangle. For this exercise, suppose the only moves allowed are to go down one row either to the left or to the right. We will code the path by using bit strings. In particular, a O will be used for each move downward to the left, and a 1 for each move downward to the right. So, for example, consider...

  • 8.26. This problem considers the various steps used in the Nelder-Mead method (a) For the top...

    8.26. This problem considers the various steps used in the Nelder-Mead method (a) For the top contour plot in Figure 8.33, explain which step from Table 8.5 was used to determine each triangle. (b) For the bottom contour plot in Figure 8.33, explain which step from Table 8.5 was used to determine each triangle. Pick a > 0 (e.g., a = 1) and B with 0 < B < a (e.g., 3 = ) Step 0 Pick 0 8<1 (e.g.,...

  • please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard...

    please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard (8 x 8 board) such that no queen is "attacking" another. Queens in chess can move vertically, horizontally, or diagonally. How you solve this problem is entirely up to you. You may choose to write a recursive program or an iterative (i.e., non-recursive) program. You will not be penalized/rewarded for choosing one method or another. Do what is easiest for you. 3.1. Output Below...

  • Python 2.7.14 Programming Assignment Shape Drawing With Notepad++(PLEASE READ AND FOLLOW THESE INSTRUCTIONS THOROUGLY AS THIS...

    Python 2.7.14 Programming Assignment Shape Drawing With Notepad++(PLEASE READ AND FOLLOW THESE INSTRUCTIONS THOROUGLY AS THIS ASSIGNMENT IS FOR PYTHON 2.7.14. ONLY AND I REALLY NEED THIS TO WORK!!! ALSO PLEASE HAVE THE CODE PROPERLY INDENTED, WITH WHATEVER VARIABLES DEFINED, WHATEVER IT TAKES TO WORK FOR PYTHON 2.7.14. I feel like nothing I do is working and I'm trying everything I can think of. ): In this assignment, the student will create a Python script that implements a series of...

  • Given an array of integers representing heights of consecutive steps, we say that a tumble is...

    Given an array of integers representing heights of consecutive steps, we say that a tumble is a sequence of strictly decreasing numbers and the height of the tumble is the difference between the height of the top step and the bottom step. For example, if there are consecutive steps with heights 16, 9, 4 then they form a tumble of height 12 (= 16-4). With consecutive steps of heights, 0, 4, 12, 4, 5, 7, 2, 1 there is a...

  • PYTHON Im stuck on this problem, PLEASE HELP! and please follow the implementation requirements! THANK YOU!...

    PYTHON Im stuck on this problem, PLEASE HELP! and please follow the implementation requirements! THANK YOU! Implement an efficient python function for the following: def find_Number_of_A11_Possible_Ways(matrix, totalGasAmount): The above function will count all the possible paths from top left cell to bottom right cell of a matrix using exactly totalGasAmount. Each cell of this matrix has a nonnegative number which represents gas amount of that cell. The constraint is that from each cell, you can either move right or move...

  • Question 2 In the minimum grid-path problem we are given a two-dimensional grid, where each point...

    Question 2 In the minimum grid-path problem we are given a two-dimensional grid, where each point on the grid has a value, which is a non-negative integer. We have to find a path from the top left corner of the grid to the bottom right corner, where each single step on the path must lead from one grid point to its neighbour to the right or below. The cost of the path is the sum of all values of the...

  • Psuedocode works! DP is dynamic programming for this algorithm Problem 4.2. (Difficulty 3) Seam carving is...

    Psuedocode works! DP is dynamic programming for this algorithm Problem 4.2. (Difficulty 3) Seam carving is a real-world application of DP for content- aware image resizing. The simplest way to reduce the size of an image is cropping and scaling, i.e. cutting out parts of the image and scaling down the size. However, cropping leaves visible crop lines of incontinuity while scaling reduces the details of the image. We would like to intelligently reducing the size while accounting for the...

  • USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku...

    USE THE PYTHON ONLY Sudoku Game Check Please write a Python program to check a Sudoku game and show its result in detail. This is an application program of using 2-dimensional arrays or lists. Each array or list is a game board of 9 x 9. Your program must check the 9 x 9 game board, and report all the problems among 9 rows, 9 columns, and 9 squares. As you can see, you must pre-load the following four games...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT