Skip to content

Commit

Permalink
solution2.5.2.10.scala provided
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Roehler <andreas.roehler@online.de>
  • Loading branch information
andreas-roehler committed Dec 2, 2024
1 parent 62bc15f commit 26dbbe5
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions chapter02/worksheets/solution2.5.2.10.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
Exercise 2.5.2.10
Transform a given sequence xs: Seq[Int] into a sequence of type
Seq[(Int, Int)] of pairs that skip one neighbor.
Implement this transformation as a function skip1 with a type
parameter A instead of the type Int.
The required type signature and a sample test:
def skip1[A](xs: Seq[A]): Seq[(A, A)] = ???
scala> skip1(List(1, 2, 3, 4, 5))
res0: List[(Int, Int)] = List((1,3), (2,4), (3,5))
*/

def skip1Intern[A](xs: Seq[A], res: Seq[(A, A)] = Seq.empty): Seq[(A, A)] = {
if (xs.length < 3) res.reverse
else
skip1Intern(xs.tail, (xs.head, xs(2)) +: res)
}

def skip1[A](xs: Seq[A]): Seq[(A, A)] = {
skip1Intern[A](xs: Seq[A])
}

val expected: List[(Int, Int)] = List((1,3), (2,4), (3,5))
val result = skip1(List(1, 2, 3, 4, 5))
assert(result == expected)

// scala> :load solution2.5.2.10.scala
// :load solution2.5.2.10.scala
// def skip1Intern[A](xs: Seq[A], res: Seq[(A, A)]): Seq[(A, A)]
// def skip1[A](xs: Seq[A]): Seq[(A, A)]
// val expected: List[(Int, Int)] = List((1,3), (2,4), (3,5))
// val result: Seq[(Int, Int)] = List((1,3), (2,4), (3,5))

0 comments on commit 26dbbe5

Please sign in to comment.