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):](http://img.homeworklib.com/questions/4e5b6960-a7b8-11ea-b216-0327fb401560.png?x-oss-process=image/resize,w_560)
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:

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

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.
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](http://img.homeworklib.com/questions/77bd9310-b879-11ea-9d14-d515a7001b95.png?x-oss-process=image/resize,w_560)
PYTHON 3 PLEASE FOLLOW INSTRUCTIONS #COMMENT STEPS :) Problem Given a triangle of integers, we want...
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 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 (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...
Iterating over a 2D array If we want to visit or process every element of a 2D array, we need a nested for loop to do it. Generally, when you iterate over a 2D array, you want to do it in row major order, because this is more memory efficient. The reason why is that adjacent elements in a row are stored contiguously (one after another) in memory. Another reason, for an English speaker, is that this is the same...
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 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!
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 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...
For this example, we are going to try and write a piece of code that counts how many black pixels are touching a white pixel. Though this may be a contrived example, you may find the code you write to be particularly valuable when we start talking about agent based models in the next class. But for now, let's worry about the problem at hand. How do we go about modeling this? If we consider some pixel, ?P, in our...
Reposting question with additional information. I am writing a java program that has the computer play a game of ConnectFour with the user, I am having trouble with the code that tells who wins the game. when I play the game after getting four in a row the program just continues to go on. I believe my FindLocalWinner method should be correct, so I need help with the findWinner method. /** * Check for a win starting at a given...