Skip to content

Commit

Permalink
Try this fix
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Roehler <[email protected]>
  • Loading branch information
andreas-roehler committed Nov 28, 2024
1 parent d069891 commit 7e24725
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions chapter02/worksheets/solution2.5.2.6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
res0: Set[(Int, Int)] = Set((2,8), (4,6), (5,5))
*/

def pairsIntern(goal: Int, xs: Array[Int], res: Seq[(Int, Int)] = Seq((0, 0))): Seq[(Int, Int)] =
def pairsIntern(goal: Int, xs: Array[Int], res: Seq[(Int, Int)] = Seq((0, 0))): Seq[(Int, Int)] = {
if (xs.isEmpty) res.reverse.tail
else
val a = xs.filter(_ + xs.head == goal)
Expand All @@ -22,8 +22,9 @@ def pairsIntern(goal: Int, xs: Array[Int], res: Seq[(Int, Int)] = Seq((0, 0))):
else
val b = (xs.head, a(0))
val c = b +: res
pairsIntern(goal, xs.tail, c)

pairsIntern(goal, xs.tail, c)
}

def pairs(goal: Int, xs: Array[Int]): Set[(Int, Int)] = {
pairsIntern(goal, xs).toSet
}
Expand Down

0 comments on commit 7e24725

Please sign in to comment.