From a9f32ad6c3f9356a3d9b8164512343cf833be41d Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 4 Nov 2024 15:47:55 +0100 Subject: [PATCH 1/4] solution2.5.2.5.scala provided Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.5.scala | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 chapter02/worksheets/solution2.5.2.5.scala diff --git a/chapter02/worksheets/solution2.5.2.5.scala b/chapter02/worksheets/solution2.5.2.5.scala new file mode 100644 index 0000000..e8c547c --- /dev/null +++ b/chapter02/worksheets/solution2.5.2.5.scala @@ -0,0 +1,39 @@ +/** + Exercise 2.5.2.5 + Same task as in Exercise 2.5.2.4 but using a set of sets. + + Instead of just three sets a, b, c, we are given a value of type + Set[Set[Int]]. + + The required type signature and a sample test: + + def prodSet(si: Set[Set[Int]]): Set[Set[Int]] = ??? + + scala> prodSet(Set(Set(1, 2), Set(3), Set(4, 5), Set(6))) + res0: Set[Set[Int]] = Set(Set(1,3,4,6),Set(1,3,5,6),Set(2,3,4,6),Set(2,3,5,6)) + + Hint: use foldLeft and flatMap. + */ + +def prodSetIntern(b: Set[Set[Int]] = Set(Set.empty), symbols: List[Int] = List.empty): Set[Set[Int]] = { + val c = b.toList + if (c.tail.isEmpty) (c.head).map{ a1 => (a1 +: symbols).toSet } + else + c.head.flatMap{ a0 => prodSetIntern(c.tail.toSet, (a0 +: symbols)) } +} + + +def prodSet(a: Set[Set[Int]] = Set(Set.empty)): Set[Set[Int]] = { + prodSetIntern(a) +} + +val result = prodSet(Set(Set(1, 2), Set(3), Set(4, 5), Set(6))) +val expected: Set[Set[Int]] = Set(Set(1,3,4,6),Set(1,3,5,6),Set(2,3,4,6),Set(2,3,5,6)) +assert(result == expected) + +// scala> :load solution2.5.2.5.scala +// :load solution2.5.2.5.scala +// def prodSetIntern(b: Set[Set[Int]], symbols: List[Int]): Set[Set[Int]] +// def prodSet(a: Set[Set[Int]]): Set[Set[Int]] +// val result: Set[Set[Int]] = Set(Set(6, 4, 3, 1), Set(6, 5, 3, 1), Set(6, 4, 3, 2), Set(6, 5, 3, 2)) +// val expected: Set[Set[Int]] = Set(Set(1, 3, 4, 6), Set(1, 3, 5, 6), Set(2, 3, 4, 6), Set(2, 3, 5, 6)) From 92163fbb35a231a66d15c969e4e6a62d38eb6e28 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 4 Nov 2024 15:51:25 +0100 Subject: [PATCH 2/4] solution2.5.2.5.scala use single symbol Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.5.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapter02/worksheets/solution2.5.2.5.scala b/chapter02/worksheets/solution2.5.2.5.scala index e8c547c..f1df08c 100644 --- a/chapter02/worksheets/solution2.5.2.5.scala +++ b/chapter02/worksheets/solution2.5.2.5.scala @@ -17,9 +17,9 @@ def prodSetIntern(b: Set[Set[Int]] = Set(Set.empty), symbols: List[Int] = List.empty): Set[Set[Int]] = { val c = b.toList - if (c.tail.isEmpty) (c.head).map{ a1 => (a1 +: symbols).toSet } + if (c.tail.isEmpty) (c.head).map{ x => (x +: symbols).toSet } else - c.head.flatMap{ a0 => prodSetIntern(c.tail.toSet, (a0 +: symbols)) } + c.head.flatMap{ x => prodSetIntern(c.tail.toSet, (x +: symbols)) } } From dd16386a30a26b3e33f078529e1f90bbeeb06c2d Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 4 Nov 2024 19:30:36 +0100 Subject: [PATCH 3/4] Use symbols: Set[Int], not List[Int] Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.5.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chapter02/worksheets/solution2.5.2.5.scala b/chapter02/worksheets/solution2.5.2.5.scala index f1df08c..c2a5327 100644 --- a/chapter02/worksheets/solution2.5.2.5.scala +++ b/chapter02/worksheets/solution2.5.2.5.scala @@ -15,11 +15,11 @@ Hint: use foldLeft and flatMap. */ -def prodSetIntern(b: Set[Set[Int]] = Set(Set.empty), symbols: List[Int] = List.empty): Set[Set[Int]] = { +def prodSetIntern(b: Set[Set[Int]] = Set(Set.empty), symbols: Set[Int] = Set.empty): Set[Set[Int]] = { val c = b.toList - if (c.tail.isEmpty) (c.head).map{ x => (x +: symbols).toSet } + if (c.tail.isEmpty) (c.head).map{ x => Set(x) ++ symbols } else - c.head.flatMap{ x => prodSetIntern(c.tail.toSet, (x +: symbols)) } + c.head.flatMap{ x => prodSetIntern(c.tail.toSet, Set(x) ++ symbols) } } @@ -33,7 +33,7 @@ assert(result == expected) // scala> :load solution2.5.2.5.scala // :load solution2.5.2.5.scala -// def prodSetIntern(b: Set[Set[Int]], symbols: List[Int]): Set[Set[Int]] +// def prodSetIntern(b: Set[Set[Int]], symbols: Set[Int]): Set[Set[Int]] // def prodSet(a: Set[Set[Int]]): Set[Set[Int]] // val result: Set[Set[Int]] = Set(Set(6, 4, 3, 1), Set(6, 5, 3, 1), Set(6, 4, 3, 2), Set(6, 5, 3, 2)) // val expected: Set[Set[Int]] = Set(Set(1, 3, 4, 6), Set(1, 3, 5, 6), Set(2, 3, 4, 6), Set(2, 3, 5, 6)) From e31fb6de30560ea114c7de85cd0e5032b2694081 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Tue, 5 Nov 2024 07:39:14 +0100 Subject: [PATCH 4/4] Don't convert Set to List Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.5.2.5.scala | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/chapter02/worksheets/solution2.5.2.5.scala b/chapter02/worksheets/solution2.5.2.5.scala index c2a5327..de86a82 100644 --- a/chapter02/worksheets/solution2.5.2.5.scala +++ b/chapter02/worksheets/solution2.5.2.5.scala @@ -16,13 +16,11 @@ */ def prodSetIntern(b: Set[Set[Int]] = Set(Set.empty), symbols: Set[Int] = Set.empty): Set[Set[Int]] = { - val c = b.toList - if (c.tail.isEmpty) (c.head).map{ x => Set(x) ++ symbols } + if (b.tail.isEmpty) (b.head).map{ x => Set(x) ++ symbols } else - c.head.flatMap{ x => prodSetIntern(c.tail.toSet, Set(x) ++ symbols) } + b.head.flatMap{ x => prodSetIntern(b.tail.toSet, Set(x) ++ symbols) } } - def prodSet(a: Set[Set[Int]] = Set(Set.empty)): Set[Set[Int]] = { prodSetIntern(a) }