From d0698913f8d1d633e26e366b908c0af59876bc20 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Wed, 27 Nov 2024 21:02:41 +0100 Subject: [PATCH 1/5] solution2.5.2.6.scala provided Signed-off-by: Andreas Roehler --- .gitignore | 1 + chapter02/worksheets/solution2.5.2.6.scala | 40 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 chapter02/worksheets/solution2.5.2.6.scala diff --git a/.gitignore b/.gitignore index 3c5ec7b..776b8ff 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *RCS* *.class *.log +chapter02/worksheets/solution2.5.2.5_foldLeft.scala # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml befehle.org diff --git a/chapter02/worksheets/solution2.5.2.6.scala b/chapter02/worksheets/solution2.5.2.6.scala new file mode 100644 index 0000000..c1d71ef --- /dev/null +++ b/chapter02/worksheets/solution2.5.2.6.scala @@ -0,0 +1,40 @@ +/** + Exercise 2.5.2.6 + + In a sorted integer array where no values are repeated, find all + pairs of values whose sum equals a given number 𝑛. + Use tail recursion. + + A type signature and a sample test: + + def pairs(goal: Int, xs: Array[Int]): Set[(Int, Int)] = ??? + + scala> pairs(10, Array(1, 2, 4, 5, 6, 8)) + 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)] = + if (xs.isEmpty) res.reverse.tail + else + val a = xs.filter(_ + xs.head == goal) + if (a.isEmpty) + pairsIntern(goal, xs.tail, res) + else + val b = (xs.head, a(0)) + val c = b +: res + pairsIntern(goal, xs.tail, c) + +def pairs(goal: Int, xs: Array[Int]): Set[(Int, Int)] = { + pairsIntern(goal, xs).toSet +} + +val expected: Set[(Int, Int)] = Set((2,8), (4,6), (5,5)) +val result = pairs(10, Array(1, 2, 4, 5, 6, 8)) +assert(result == expected) + +// scala> :load solution2.5.2.6.scala +// :load solution2.5.2.6.scala +// def pairsIntern(goal: Int, xs: Array[Int], res: Seq[(Int, Int)]): Seq[(Int, Int)] +// def pairs(goal: Int, xs: Array[Int]): Set[(Int, Int)] +// val expected: Set[(Int, Int)] = Set((2,8), (4,6), (5,5)) +// val result: Set[(Int, Int)] = Set((2,8), (4,6), (5,5)) From 7e24725289cdeec60a9098e0213d5e4f78591196 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Thu, 28 Nov 2024 11:02:09 +0100 Subject: [PATCH 2/5] Try this fix Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.6.scala | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/chapter02/worksheets/solution2.5.2.6.scala b/chapter02/worksheets/solution2.5.2.6.scala index c1d71ef..36eb4f7 100644 --- a/chapter02/worksheets/solution2.5.2.6.scala +++ b/chapter02/worksheets/solution2.5.2.6.scala @@ -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) @@ -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 } From 372c6ddf23f0ca37c9263276cc4d4022df2a1cb1 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Thu, 28 Nov 2024 12:05:44 +0100 Subject: [PATCH 3/5] another try Signed-off-by: Andreas Roehler --- .gitignore | 1 + chapter02/worksheets/solution2.5.2.6.scala | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 776b8ff..2c63667 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ chapter02/worksheets/solution2.5.2.5_foldLeft.scala # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +auspack/ befehle.org hs_err_pid* target diff --git a/chapter02/worksheets/solution2.5.2.6.scala b/chapter02/worksheets/solution2.5.2.6.scala index 36eb4f7..a9c9665 100644 --- a/chapter02/worksheets/solution2.5.2.6.scala +++ b/chapter02/worksheets/solution2.5.2.6.scala @@ -19,9 +19,10 @@ def pairsIntern(goal: Int, xs: Array[Int], res: Seq[(Int, Int)] = Seq((0, 0))): val a = xs.filter(_ + xs.head == goal) if (a.isEmpty) pairsIntern(goal, xs.tail, res) - else + else { val b = (xs.head, a(0)) val c = b +: res + } pairsIntern(goal, xs.tail, c) } From 7bcd7876ca602ceae0e12f05cd7dba32b85c6b02 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Thu, 28 Nov 2024 12:09:28 +0100 Subject: [PATCH 4/5] another else Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.6.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/chapter02/worksheets/solution2.5.2.6.scala b/chapter02/worksheets/solution2.5.2.6.scala index a9c9665..73dc2ce 100644 --- a/chapter02/worksheets/solution2.5.2.6.scala +++ b/chapter02/worksheets/solution2.5.2.6.scala @@ -15,14 +15,16 @@ def pairsIntern(goal: Int, xs: Array[Int], res: Seq[(Int, Int)] = Seq((0, 0))): Seq[(Int, Int)] = { if (xs.isEmpty) res.reverse.tail - else + else { val a = xs.filter(_ + xs.head == goal) if (a.isEmpty) pairsIntern(goal, xs.tail, res) else { val b = (xs.head, a(0)) val c = b +: res + } + } pairsIntern(goal, xs.tail, c) } From 2f791debd23dad8b05df3f361b6426511c332a35 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Thu, 28 Nov 2024 12:13:49 +0100 Subject: [PATCH 5/5] braces fixed Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.6.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/chapter02/worksheets/solution2.5.2.6.scala b/chapter02/worksheets/solution2.5.2.6.scala index 73dc2ce..fe0599c 100644 --- a/chapter02/worksheets/solution2.5.2.6.scala +++ b/chapter02/worksheets/solution2.5.2.6.scala @@ -22,10 +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)] = {