Problem
Spreadsheet cells are referenced by column and row identifiers. Columns are labeled with alphabetical characters, starting with "A", "B", "C", ...; rows are numbered from 1 in ascending order. Write a function that takes in a string identifying a range of cells in a spreadsheet and returns an ordered list of cells which compose that range.
Example:
"A3:D5" -> ["A3", "A4", "A5", "B3", "B4", "B5", "C3", "C4", "C5", "D3", "D4", "D5"]
"A3:D4" -> ["A3", "A4", "B3", "B4", "B5", "C3", "C4", "C5", "D3", "D4"]
Here is scala implementation of the same ,
import scala.language.postfixOps
object PrintSpreadSheet extends App {
val validAlphabets = ('A' to 'Z').toSeq
def cells(range: String): Seq[String] = {
val corners = (range split ":") flatMap { corner =>
Seq(corner.head, corner.last)
}
val rows = (corners filter (r => validAlphabets.contains(r))) sorted
val cols = (corners filter (c => !validAlphabets.contains(c))) sorted
(rows.head to rows.last) flatMap { r =>
(cols.head to cols.last) map { c =>
r.toString + ":" + c.toString
}
}
}
cells("A1:D5") foreach println
}