Alternative solution for RecursionPatternMatchingExercise.zipMultiple

5 views
Skip to first unread message

Channing

unread,
Jun 21, 2011, 3:00:48 PM6/21/11
to scala-labs
Hi,
I think I have a smaller solution to the
RecursionPatternMatchingExercise.zipMultiple(WithDifferentSize)
exercises, at least the tests pass.

def zipMultiple(in: List[List[_]]): List[List[_]] = {
in match {
case Nil :: _ => Nil
case _ => {
val heads = in.map(_.head)
val tails = in.map(_.tail)
heads :: zipMultiple(tails)
}
}
}


def zipMultipleWithDifferentSize(in: List[List[_]]): List[List[_]] = {
val minLength = in.minBy(_.size).size
val chopped = in.map(_.take(minLength))
zipMultiple(chopped)
}

Jeroen van Erp

unread,
Jun 23, 2011, 3:45:49 AM6/23/11
to scala...@googlegroups.com
Hi Channing,

That also seems to do the trick indeed, smart solution!

Regards,
Jeroen

Channing Walton

unread,
Jun 23, 2011, 4:02:41 AM6/23/11
to scala...@googlegroups.com
well it took me an hour!! I hope to get quicker with this stuff :)
Reply all
Reply to author
Forward
0 new messages