From 2e3cb6979b2498e3590b63c51ab2c62d970fc126 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Wed, 6 Dec 2023 13:44:01 +0100 Subject: [PATCH 1/3] #31, solution2.1.7.8 provided Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.1.7.8.scala | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 chapter02/worksheets/solution2.1.7.8.scala diff --git a/chapter02/worksheets/solution2.1.7.8.scala b/chapter02/worksheets/solution2.1.7.8.scala new file mode 100644 index 0000000..9b07735 --- /dev/null +++ b/chapter02/worksheets/solution2.1.7.8.scala @@ -0,0 +1,32 @@ +/** author: Andreas Röhler */ + +/** + Exercise 2.1.7.8 + + Write the solution of Exercise 2.1.7.7 as a function with type + parameters P and Q instead of the fixed types String and Int. Test + it with types P = Boolean and Q = Set[Int]. + */ + +/** + Exercise 2.1.7.7 + + Given p:Seq[String] and q:Seq[Int] of equal length and assuming that + values in q do not repeat, compute a Map[Int, String] mapping numbers + from q to the corresponding strings from p. + */ + +// def reorder[A](p: Seq[A], q: Seq[Int]): Seq[A] = ??? +def myMapping[P,Q](p: Seq[P], q: Seq[Q]): Map[Q, P] = { + val erg = q.zip(p).toMap + erg + } + +val result = myMapping(Seq("asdf", "bsdf", "csdf", "dsdf"), Seq(1, 2, 3, 4)) + +val expected = Map(1 -> "asdf", 2 -> "bsdf", 3 -> "csdf", 4 -> "dsdf") +assert(result == expected) + + + // val p: Seq[P] = Seq("asdf", "bsdf", "csdf", "dsdf") + // val q: Seq[Int] = (1 to p.length) From 1113542c18c0b5a5f8a890595e471a6415daad95 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Thu, 7 Dec 2023 11:22:45 +0100 Subject: [PATCH 2/3] solution2.1.7.8 fixed Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.1.7.8.scala | 31 ++++++++++------------ 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/chapter02/worksheets/solution2.1.7.8.scala b/chapter02/worksheets/solution2.1.7.8.scala index 9b07735..c0c51e8 100644 --- a/chapter02/worksheets/solution2.1.7.8.scala +++ b/chapter02/worksheets/solution2.1.7.8.scala @@ -1,12 +1,5 @@ /** author: Andreas Röhler */ - -/** - Exercise 2.1.7.8 - - Write the solution of Exercise 2.1.7.7 as a function with type - parameters P and Q instead of the fixed types String and Int. Test - it with types P = Boolean and Q = Set[Int]. - */ +/** author: Sergei Winitzki */ /** Exercise 2.1.7.7 @@ -16,17 +9,21 @@ from q to the corresponding strings from p. */ -// def reorder[A](p: Seq[A], q: Seq[Int]): Seq[A] = ??? -def myMapping[P,Q](p: Seq[P], q: Seq[Q]): Map[Q, P] = { - val erg = q.zip(p).toMap +/** + Exercise 2.1.7.8 + Write the solution of Exercise 2.1.7.7 as a function with + type parameters P and Q instead of the fixed types Int and String. + + The return type of the function should be Map[P, Q]. Run some tests + using types P = Double and Q = Set[Boolean]. */ + +def myMapping[P,Q](p: Seq[P], q: Seq[Q]): Map[P, Q] = { + val erg: Map[P, Q] = p.zip(q).toMap erg } -val result = myMapping(Seq("asdf", "bsdf", "csdf", "dsdf"), Seq(1, 2, 3, 4)) +val result = myMapping(Seq( Set(true), Set(true, false), Set() ), Seq( 1.0, 2.0, 3.0)) -val expected = Map(1 -> "asdf", 2 -> "bsdf", 3 -> "csdf", 4 -> "dsdf") -assert(result == expected) +val expected: Map[scala.collection.immutable.Set[_ <: Boolean],Double] = Map(Set(true) -> 1.0, Set(true, false) -> 2.0, Set() -> 3.0) - - // val p: Seq[P] = Seq("asdf", "bsdf", "csdf", "dsdf") - // val q: Seq[Int] = (1 to p.length) +assert(result == expected) From a08f216225e39bf90a57f48e085a6a106c02c0c5 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Thu, 7 Dec 2023 13:11:46 +0100 Subject: [PATCH 3/3] solution2.1.7.8.scala again Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.1.7.8.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter02/worksheets/solution2.1.7.8.scala b/chapter02/worksheets/solution2.1.7.8.scala index c0c51e8..11e6055 100644 --- a/chapter02/worksheets/solution2.1.7.8.scala +++ b/chapter02/worksheets/solution2.1.7.8.scala @@ -24,6 +24,6 @@ def myMapping[P,Q](p: Seq[P], q: Seq[Q]): Map[P, Q] = { val result = myMapping(Seq( Set(true), Set(true, false), Set() ), Seq( 1.0, 2.0, 3.0)) -val expected: Map[scala.collection.immutable.Set[_ <: Boolean],Double] = Map(Set(true) -> 1.0, Set(true, false) -> 2.0, Set() -> 3.0) +val expected: Map[Set[Boolean],Double] = Map(Set(true) -> 1.0, Set(true, false) -> 2.0, Set() -> 3.0) assert(result == expected)