Maximum triangle path sum: Difference between revisions
Content added Content deleted
(→{{header|Perl 6}}: more explanation) |
(→{{header|Perl 6}}: supply right-associative solution) |
||
Line 69: | Line 69: | ||
{{out}} |
{{out}} |
||
<pre>1320</pre> |
<pre>1320</pre> |
||
Here's a more FPish version with the same output. We define our own operator and the use it in the reduction metaoperator form, <tt>[op]</tt>, which turns any infix into a list operator. |
Here's a more FPish version with the same output. We define our own operator and the use it in the reduction metaoperator form, <tt>[op]</tt>, which turns any infix into a list operator. |
||
<lang perl6>sub infix:<op>(@a,@b) { (@a Zmax @a.rotate) Z+ @b } |
<lang perl6>sub infix:<op>(@a,@b) { (@a Zmax @a.rotate) Z+ @b } |
||
say [op] slurp("triangle.txt").lines.reverse.map: { [.words] }</lang> |
say [op] slurp("triangle.txt").lines.reverse.map: { [.words] }</lang> |
||
Instead of using reverse, one could also define the op as right-associative. |
|||
<lang perl6>sub infix:<op>(@a,@b) is assoc('right') { @a Z+ (@b Zmax @b.rotate) } |
|||
say [op] slurp("triangle.txt").lines.map: { [.words] }</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |