user5607337
user5607337

Reputation:

Workout with scala and algorithms

Want to improve my algorithmic skills. For example, task like this

Need to merge 2 lists by the next rule

* ListA = 1 2 4 6 100
* ListB = 5 200
* ListRes = 1 2 4 5 6 100 200 

Here is implementation

  @tailrec
  def mergeList(a: List[Int], b: List[Int], res: List[Int]): List[Int] = {
    if (a.isEmpty) return res ::: b
    if (b.isEmpty) return res ::: a
    if (a.head < b.head) mergeList(a.tail, b, res ::: List(a.head)) else
    mergeList(a, b.tail, res ::: List(b.head))
  }

unit test for checking:

  test("testMergeList") {
    val a = List(1, 6, 7, 8, 40)
    val b = List(1, 7, 8, 9, 11, 20, 100)

    println(Workout.mergeList(a, b, List()))
    println(Workout.mergeList(b, a, List()))

    assert(Workout.mergeList(a, b, List()) equals List(1, 1, 6, 7, 7, 8, 8, 9, 11, 20, 40, 100))
    assert(Workout.mergeList(b, a, List()) equals List(1, 1, 6, 7, 7, 8, 8, 9, 11, 20, 40, 100))
  }

Could you please recommend some resources with tasks like this ?

Where can I post and discus my implementation ?

Thanks.

Upvotes: 2

Views: 410

Answers (2)

elm
elm

Reputation: 20415

Try the Euler Project which challenges your algorithmic skills as well as your understanding of a programming language of your choice, for instance Scala.

Also Scala for the Impatient book includes numerous exercises.

Upvotes: 0

marios
marios

Reputation: 8996

You can use Hacker Rank for many (not always functional) problems to solve.

Also the red book (Functional Programming in Scala) has great problems with solutions on github.

Finally, needless to say that answering SO questions is another great way to get what you want.

Upvotes: 1

Related Questions