Last active
August 29, 2015 14:07
-
-
Save ajkamel/0b368a9a88d5a018714f to your computer and use it in GitHub Desktop.
Triangle Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| I first found this problem when looking at a Yodle engineering challenge question. | |
| Basically you this takes a file with lines of numbers and finds the largest sum by totaling the largest adjacent numbers for each new line. | |
| ``` | |
| triangle_array = [] | |
| f = File.open("triangle.txt", "r") | |
| f.each_line do |line| | |
| triangle_array << line.split.map(&:to_i) | |
| end | |
| f.close | |
| lines = triangle_array.length | |
| for i in (lines-2).downto(0).to_a do | |
| for j in (i).downto(0).to_a do | |
| triangle_array[i][j] += [triangle_array[i+1][j].to_i,triangle_array[i+1][j+1].to_i].max | |
| end | |
| end | |
| p triangle_array[0][0] | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment