{"id":1698,"date":"2013-07-29T18:07:44","date_gmt":"2013-07-29T18:07:44","guid":{"rendered":"http:\/\/justthesam.com\/2013\/07\/equilibrium-coding-challenge-a-scala-solution\/"},"modified":"2013-07-29T18:07:44","modified_gmt":"2013-07-29T18:07:44","slug":"equilibrium-coding-challenge-a-scala-solution","status":"publish","type":"post","link":"https:\/\/justthesam.com\/?p=1698","title":{"rendered":"Equilibrium coding challenge &#8211; a Scala solution"},"content":{"rendered":"<p>Inspired by a random tweet about their added Scala support, I tried out <a href=\"http:\/\/g.codility.com\/demo\/take-sample-test\/\" target=\"_self\" rel=\"noopener\">the Codility sample test<\/a>. I rather liked my solution and I think it&#8217;s a perfect example of some of the niceties of Scala, in a small way, so here it is:<\/p>\n<pre class=\"prettyprint\">def solution(a: Array[Int]): Int = {\n  \/\/ Partial sums are Longs to avoid Int overflow.\n  val sums = a.scanLeft(0L)(_ + _).tail\n  def equilibrium(index: Int) = leftSum(index) == rightSum(index)\n  def leftSum(index: Int) = if (index == 0) 0 else sums(index - 1)\n  def rightSum(index: Int) = sums.last - sums(index)\n  (0 until a.length) find (equilibrium(_)) getOrElse(-1)\n}<\/pre>\n<p>The use of scanLeft to build up all the partial sums is particularly handy. Having done that it&#8217;s very easy to run through all the indexes until we find one that satisfies us. Note that the find method returns an Option[A] so we use getOrElse to return -1 if no solution was found (as per the requirements).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Inspired by a random tweet about their added Scala support, I tried out the Codility sample test. I rather liked my solution and I think it&#8217;s a perfect example of some of the niceties of Scala, in a small way, so here it is: def solution(a: Array[Int]): Int = { \/\/ Partial sums are Longs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,24,33],"tags":[],"class_list":["post-1698","post","type-post","status-publish","format-standard","hentry","category-programming","category-scala","category-webtech"],"_links":{"self":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1698"}],"version-history":[{"count":0,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1698\/revisions"}],"wp:attachment":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}