Scala algorithm: Compute missing ranges
Given a set of numbers within a range, and the specified range, find the gaps that need to be filled.
Test cases in Scala
Algorithm in Scala
11 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!
We use LazyLists to be able to handle possibly a larger number of items for a potentially large range.
Then, to discover the ranges that are missing, we use a Sliding / Sliding Window to find candidates and then filter them out. (this is © from www.scala-algorithms.com)
Scala concepts & Hints
Drop, Take, dropRight, takeRight
Scala's `drop` and `take` methods typically remove or select `n` items from a collection.
The for-comprehension is highly important syntatic enhancement in functional programming languages.
The 'LazyList' type (previously known as 'Stream' in Scala) is used to describe a potentially infinite list that evaluates only when necessary ('lazily').
(1 to n)syntax produces a "Range" which is a representation of a sequence of numbers.
Sliding / Sliding Window
Get fixed-length sliding sub-sequences (sliding windows) from another sequence