From 9f3faba1e0b63fb201b17fbe9c218ef6840d9b8f Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Sun, 10 May 2020 19:21:07 +0200 Subject: [PATCH] Add second solution for problem 5 task 1 --- Contest2020/Contest2020.dyalog | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Contest2020/Contest2020.dyalog b/Contest2020/Contest2020.dyalog index 7e53ff5..0834c30 100644 --- a/Contest2020/Contest2020.dyalog +++ b/Contest2020/Contest2020.dyalog @@ -67,6 +67,23 @@ r←((↑∘amounts)((1+⊢)⊥⊣)(↑∘rates))¨⍳⍴rates ∇ + ∇ r←amounts rr2 rates;recur + ⍝ Second solution + ⍝ The recurrence relation (⍺ is the previous value, + ⍝ ⍵[1] is the current amount, and ⍵[2] is the + ⍝ current rate). + recur←{⍵[1]+⍺×1+⍵[2]} + ⍝ Because APL evaluates from right to left, we can't + ⍝ use Scan directly, as recur is not associative. We + ⍝ need something like Haskell's scanl, that will + ⍝ accumulate left-to-right and accumulate + ⍝ left-to-right. Scan accumulates left-to-right but + ⍝ evaluates right-to-left. This solution therefore + ⍝ folds for all subarrays, but it is not good in + ⍝ terms of performance. + r←{⊃⊃f⍨/(⌽⍵↑amounts,¨rates),⊂0 0}¨⍳⍴amounts + ∇ + ∇ r←cashFlow pv rates ⍝ 2020 APL Problem Solving Competition Phase II ⍝ Stub function for Problem 5, Task 2 - pv