99 Scala Problems Index

I decided to learn Scala. I looked for some good exercises for Scala programmers and found S-99: Ninety-Nine Scala Problems, and since writing about things helps me learning them I am going to write a post for each problem I manage to solve.

The official site provides solutions for the problems, so my solutions will obviously be influenced by those, when not the same.

CAVEAT: I am a beginner so what I state in the following posts may be inaccurate and sometimes wrong. Please submit an issue on the blog GitHub issues page.

This is the index of the problems I already discussed. I kept the original title and difficulty ranking (asterisks)





99 Scala Problems 06 - Find out whether a list is a palindrome

The problem

P06 (*) Find out whether a list is a palindrome.

Example:

scala> isPalindrome(List(1, 2, 3, 2, 1))
res0: Boolean = true

Initial thoughts

The algorithm is pretty simple. A list is palindrome when it is equal to its reversed version. So both a procedural and a recursive solution may get a reversed version of the list and then compare the two.

For long lists this could be a performance issue, so a ... more



99 Scala Problems 04 - Find the number of elements of a list

The problem

P04 (*) Find the number of elements of a list.

Example:

scala> length(List(1, 1, 2, 3, 5, 8))
res0: Int = 6

Initial thoughts

This should be straightforward both with object-oriented and functional techniques. There are no special cases except that of an empty list. There is an interesting solution that mixes functional and object-oriented techniques, which is ... more



99 Scala Problems 02 - Find the last but one element of a list

The problem

P02 (*) Find the last but one element of a list.

Example:

scala> penultimate(List(1, 1, 2, 3, 5, 8))
res0: Int = 5

Initial thoughts

This problem come in the same form as the first one, so most of the syntax issues have already been solved. The challenge here is to find a good algorithm to extract the penultimate element and, as done for the first problem, it ... more


99 Scala Problems 01 - Find the last element of a list

The problem

P01 (*) Find the last element of a list.

Example:

scala> last(List(1, 1, 2, 3, 5, 8))
res0: Int = 8

Initial thoughts

This is the very first Scala problem, but a lot of topics are covered even by such a small task.

First I have to learn how to define functions. Then the problem shows two data types, namely List and Int, but it ... more