Daily Coding Problems in Scala (5 Part Series)
Daily Coding Problem is a website which will send you a programming challenge to your inbox every day. I want to show beginners how to solve some of these problems using Scala, so this will be an ongoing series of my solutions. Feel free to pick them apart in the comments!
Given a list of numbers and a number
k, return whether any two numbers from the list add up to
For example, given
[10, 15, 3, 7]and
10 + 7is
Bonus: Can you do this in one pass?
Rather than using nested loops in an imperative style, as I did in my Java solution, I'd like to stick with a more functional approach for my Scala solution to this problem.
Scala provides a beautiful standard library, including
Lists with methods like
combinations(n: Int) (which gives all combinations of
n elements in the
exists(p: List[T] => Boolean), which returns
true if there exists at least one element in the list which satisfies the given predicate,
p. Combined with Scala's
implicit parameters and
Numeric type class under the hood (neither of which exist in Java), we can solve this problem in a single line of Scala code (not including the definitions of the list and
val ll: List[Int] = List(10, 15, 3, 7) val k: Int = 17 ll.combinations(2).exists(_.sum == k) // returns true
ll.combinations(2) returns a
List[List[Int]]. That is, a
List which contains
List[Int] (lists of ints). The inner lists are all of the possible distinct pairs of elements of the list
exists(...) on a
List checks if any element of the list satisfies the function passed as the argument to
exists(...). (Note that this requires Scala's support for higher-order functions.) The function I passed,
_.sum = k, checks each inner list (each element of
ll) using the underscore
_ as a placeholder for each element in turn. Since each element is itself a
List[Int], we can call the
sum function on that inner
List, and simply check if the sum is equal to
Removing all this syntactic sugar, you might end up with something like:
// with syntactic sugar ll.combinations(2).exists(_.sum == k) // de-sugared ll.combinations(2).exists( (innerList: List[Int]) => innerList.sum == k )
...but the Scala compiler knows that the type of
innerList must be
ll is of type
List[List[Int]], so its elements are
List[Int]s. So we can drop the explicit type annotation:
ll.combinations(2).exists( (innerList) => innerList.sum == k )
...and when we declare an anonymous function with only a single argument, as above, we can drop the parentheses around that argument:
ll.combinations(2).exists( innerList => innerList.sum == k )
Finally, we can replace any number of anonymous function arguments with underscores
_, and the Scala compiler will replace them, one by one, in the function definition:
ll.combinations(2).exists(_.sum == k)
The end result of all of this syntactic sugar is a compact bit of code that is easy to read and error check. You can see why I like Scala so much, especially compared to the verbosity of my (not purposefully obfuscated) Java solution!
All the code for my Daily Coding Problems solutions is available at github.com/awwsmm/daily.
Suggestions? Let me know in the comments.
If you enjoyed this post, please consider supporting my work by buying me a coffee!