Skip to content

Instantly share code, notes, and snippets.

@pathikrit
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save pathikrit/ffb8a05cd9cd7414c2c2 to your computer and use it in GitHub Desktop.

Select an option

Save pathikrit/ffb8a05cd9cd7414c2c2 to your computer and use it in GitHub Desktop.
Natural weekday chooser
val weekdays = IndexedSeq("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
def solve(idx: Int*) = (idx foldLeft Seq.empty[(Int, Int)]) {
case (ts :+ ((start, end)), x) if x == end + 1 => ts :+ (start, x)
case (t, x) => t :+ (x, x)
} map {
case (start, end) if start == end => weekdays(start)
case (start, end) => s"${weekdays(start)}-${weekdays(end)}"
}
println(solve(1, 2, 3, 5))
println(solve(1, 2, 5))
println(solve(2, 3, 4))
println(solve(1, 3, 5))
println(solve(0, 1, 3, 5, 6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment