How to resolve the algorithm Maximum triangle path sum step by step in the Sidef programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Maximum triangle path sum step by step in the Sidef programming language
Table of Contents
Problem Statement
Starting from the top of a pyramid of numbers like this, you can walk down going one step on the right or on the left, until you reach the bottom row: One of such walks is 55 - 94 - 30 - 26. You can compute the total of the numbers you have seen in such walk, in this case it's 205. Your problem is to find the maximum total among all possible paths from the top to the bottom row of the triangle. In the little example above it's 321.
Find the maximum total in the triangle below: Such numbers can be included in the solution code, or read from a "triangle.txt" file. This task is derived from the Euler Problem #18.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Maximum triangle path sum step by step in the Sidef programming language
Source code in the sidef programming language
var sum = [0]
ARGF.each { |line|
var x = line.words.map{.to_n}
sum = [
x.first + sum.first,
1 ..^ x.end -> map{|i| x[i] + [sum[i-1, i]].max}...,
x.last + sum.last,
]
}
say sum.max
var triangle = ARGF.slurp.lines.map{.words.map{.to_n}}
func max_value(i=0, j=0) is cached {
i == triangle.len && return 0
triangle[i][j] + [max_value(i+1, j), max_value(i+1, j+1)].max
}
say max_value()
You may also check:How to resolve the algorithm Loops/While step by step in the EMal programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Python programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the Nim programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the AWK programming language
You may also check:How to resolve the algorithm Textonyms step by step in the Factor programming language