Skip to content

Commit

Permalink
Merge pull request #32 from andreas-roehler/master
Browse files Browse the repository at this point in the history
#31, solution2.1.7.8 provided
  • Loading branch information
winitzki authored Dec 7, 2023
2 parents 3581eb1 + a08f216 commit f6e2790
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions chapter02/worksheets/solution2.1.7.8.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** author: Andreas Röhler */
/** author: Sergei Winitzki */

/**
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.
*/

/**
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( Set(true), Set(true, false), Set() ), Seq( 1.0, 2.0, 3.0))

val expected: Map[Set[Boolean],Double] = Map(Set(true) -> 1.0, Set(true, false) -> 2.0, Set() -> 3.0)

assert(result == expected)

0 comments on commit f6e2790

Please sign in to comment.