From 64a0f3709e477767f2aa7e9e286c8a28f4df30ee Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 8 Jan 2024 12:03:29 +0100 Subject: [PATCH 1/7] solution1.6.2.3 added Signed-off-by: Andreas Roehler --- chapter01/worksheets/solution1.6.2.3.scala | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 chapter01/worksheets/solution1.6.2.3.scala diff --git a/chapter01/worksheets/solution1.6.2.3.scala b/chapter01/worksheets/solution1.6.2.3.scala new file mode 100644 index 0000000..c9f7faa --- /dev/null +++ b/chapter01/worksheets/solution1.6.2.3.scala @@ -0,0 +1,23 @@ +/** author: Andreas Röhler */ + +/** + Exercise 1.6.2.3 + Given a function f: Int => Boolean, an integer n is called a “3- f ” +if there are only three different integers j ∈ [1, ..., n] such that f +( j) returns true. Define a function that takes f as an argument and +returns a sequence of all “3- f ” integers among n ∈ [1, ..., 1000]. +What is the type of that function? Implement Exercise 1.6.2.2 using +that function. + */ + +def is3Factor(n: Int): Boolean = { + val q = (2 to n-1) + val r = q.filter(y => (n != y) && (n % y == 0)) + (r.length == 3) +} + +def nestedIs3Factor(f: (Int => Boolean)): List[Int] = { + (1 to 1000).toList.filter(x => f(x)) +} + +nestedIs3Factor(is3Factor) From e914f2dbe88ba06a6158eca64e57215d381a3d80 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 8 Jan 2024 13:46:02 +0100 Subject: [PATCH 2/7] solution1.6.2.4 added Signed-off-by: Andreas Roehler --- chapter01/worksheets/solution1.6.2.4.scala | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 chapter01/worksheets/solution1.6.2.4.scala diff --git a/chapter01/worksheets/solution1.6.2.4.scala b/chapter01/worksheets/solution1.6.2.4.scala new file mode 100644 index 0000000..a160370 --- /dev/null +++ b/chapter01/worksheets/solution1.6.2.4.scala @@ -0,0 +1,22 @@ +/** author: Andreas Röhler */ + +/** + Exercise 1.6.2.4 + Define a function see100 of type List[List[Int]] => List[List[Int]] + that selects only those inner lists whose largest value is at least + 100. + + Test with: + scala> see100( List( List(0, 1, 100), List(60, 80), List(1000) ) ) + res0: List[List[Int]] = List(List(0, 1, 100), List(1000)) + */ + +def see100(a: List[List[Int]]): List[List[Int]] = { + a.filter(99 < _.max) +} + +val result = see100( List( List(0, 1, 100), List(60, 80), List(1000) ) ) + +val expected:List[List[Int]] = List(List(0, 1, 100), List(1000)) + +assert(result == expected) From 1cd1ac9424b572f19bd2c3fba418cd47c24d9fc0 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 8 Jan 2024 14:18:18 +0100 Subject: [PATCH 3/7] #84, solution1.6.2.5 provided Signed-off-by: Andreas Roehler --- chapter01/worksheets/solution1.6.2.5.scala | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 chapter01/worksheets/solution1.6.2.5.scala diff --git a/chapter01/worksheets/solution1.6.2.5.scala b/chapter01/worksheets/solution1.6.2.5.scala new file mode 100644 index 0000000..239f1b8 --- /dev/null +++ b/chapter01/worksheets/solution1.6.2.5.scala @@ -0,0 +1,24 @@ +2/** author: Andreas Röhler */ + +/** + Exercise 1.6.2.5 + Define a function of type List[Double] => List[Double] that “normalizes” the list: + + it finds the element having the largest absolute value and, if that +value is nonzero, divides all elements by that value and returns a new + list; otherwise returns the original list. + + Test with: scala> normalize(List(1.0, -4.0, 2.0)) + res0: List[Double] = List(0.25, 1.0, 0.5) + */ + +def normalize(a: List[Double]): List[Double] = { + + val b = a.map(k => k.abs).max + if (b == 0) a else a.map(k => k.abs).map(_ / 4) +} + +val result = normalize(List(1.0, -4.0, 2.0)) +val expected: List[Double] = List(0.25, 1.0, 0.5) + +assert(result == expected) From f811e6f79a941c36ed15a7b059007a1bec9aaa01 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 8 Jan 2024 16:57:32 +0100 Subject: [PATCH 4/7] Honor PR comment Signed-off-by: Andreas Roehler --- chapter01/worksheets/solution1.6.2.3.scala | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/chapter01/worksheets/solution1.6.2.3.scala b/chapter01/worksheets/solution1.6.2.3.scala index c9f7faa..d22e25c 100644 --- a/chapter01/worksheets/solution1.6.2.3.scala +++ b/chapter01/worksheets/solution1.6.2.3.scala @@ -17,7 +17,10 @@ def is3Factor(n: Int): Boolean = { } def nestedIs3Factor(f: (Int => Boolean)): List[Int] = { - (1 to 1000).toList.filter(x => f(x)) + (1 to 1000).filter(x => f(x)).toList } -nestedIs3Factor(is3Factor) +val result = nestedIs3Factor(is3Factor) +val expected = Vector(16, 81, 625) + +assert(result == expected) From 5ce3e3650f5af7660854668cc878d4ce33882b07 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 8 Jan 2024 16:59:17 +0100 Subject: [PATCH 5/7] Typo fixed Signed-off-by: Andreas Roehler --- chapter01/worksheets/solution1.6.2.5.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter01/worksheets/solution1.6.2.5.scala b/chapter01/worksheets/solution1.6.2.5.scala index 239f1b8..8269959 100644 --- a/chapter01/worksheets/solution1.6.2.5.scala +++ b/chapter01/worksheets/solution1.6.2.5.scala @@ -1,4 +1,4 @@ -2/** author: Andreas Röhler */ +/** author: Andreas Röhler */ /** Exercise 1.6.2.5 From 5de0d988851deac268ba45ed2fecee5ad43f296e Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 8 Jan 2024 20:21:17 +0100 Subject: [PATCH 6/7] solution1.6.2.5 fixed Signed-off-by: Andreas Roehler --- chapter01/worksheets/solution1.6.2.5.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chapter01/worksheets/solution1.6.2.5.scala b/chapter01/worksheets/solution1.6.2.5.scala index 8269959..8efe74f 100644 --- a/chapter01/worksheets/solution1.6.2.5.scala +++ b/chapter01/worksheets/solution1.6.2.5.scala @@ -5,20 +5,20 @@ Define a function of type List[Double] => List[Double] that “normalizes” the list: it finds the element having the largest absolute value and, if that -value is nonzero, divides all elements by that value and returns a new + value is nonzero, divides all elements by that value and returns a new list; otherwise returns the original list. Test with: scala> normalize(List(1.0, -4.0, 2.0)) - res0: List[Double] = List(0.25, 1.0, 0.5) + res0: List[Double] = List(0.25, -1.0, 0.5) */ def normalize(a: List[Double]): List[Double] = { val b = a.map(k => k.abs).max - if (b == 0) a else a.map(k => k.abs).map(_ / 4) + if (b == 0) a else a.map(_ / 4) } val result = normalize(List(1.0, -4.0, 2.0)) -val expected: List[Double] = List(0.25, 1.0, 0.5) +val expected: List[Double] = List(0.25, -1.0, 0.5) assert(result == expected) From 5f84f1ee15d8bb82208e28aa3d6d7ee9f64ad3d4 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Tue, 9 Jan 2024 09:33:08 +0100 Subject: [PATCH 7/7] fix hard-coded 4 Signed-off-by: Andreas Roehler --- chapter01/worksheets/solution1.6.2.5.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter01/worksheets/solution1.6.2.5.scala b/chapter01/worksheets/solution1.6.2.5.scala index 8efe74f..314efc7 100644 --- a/chapter01/worksheets/solution1.6.2.5.scala +++ b/chapter01/worksheets/solution1.6.2.5.scala @@ -15,7 +15,7 @@ def normalize(a: List[Double]): List[Double] = { val b = a.map(k => k.abs).max - if (b == 0) a else a.map(_ / 4) + if (b == 0) a else a.map(_ / b) } val result = normalize(List(1.0, -4.0, 2.0))