Problem overview
The method fileExtension receives a file name and returns its extension: the part after the last dot. "photo.jpeg" gives "jpeg". A name can hold several dots, and only the last one counts: "archive.tar.gz" gives "gz". The extension keeps its letters exactly as written, so "README.MD" gives "MD". The lesson guarantees at least one dot, and that the name neither starts nor ends with a dot.
Approach
Cut the name at every dot, and the extension is the last of the pieces. This works whether the name has one dot or five, because the last piece is always whatever follows the last dot. It means the method needs two things: a way to split a string, and a way to read the last item of the result.
The split has one trap. String.split doesn't take a plain separator: it takes a regular expression, and in a regular expression a lone . means "any character". Splitting "archive.tar.gz" with split(".") treats every character as a separator, and the result is an empty array, with length 0. To cut at real dots, the dot has to be escaped with a backslash, and the backslash itself has to be doubled inside a Scala string literal: "\\.".
Step by step
- Split the name at the dots with
fileName.split("\\.")and keep the result in avalnamedparts. For"archive.tar.gz"it is the array[archive, tar, gz]. - Read the last item. Scala reads an array item with parentheses,
parts(i), and the indexes start at 0. An array ofparts.lengthitems ends at indexparts.length - 1, so that is where the extension is. Return it.
The solution in Scala
class Solution {
def fileExtension(fileName: String): String = {
val parts = fileName.split("\\.")
return parts(parts.length - 1)
}
}
A few Scala details:
splitreturns anArray[String]. Arrays are read withparts(i), not with square brackets, which Scala uses for type parameters."\\."is two characters, a backslash and a dot, once the string literal is read. The regular expression engine then sees\., a literal dot.valmakespartsa name that can't be reassigned. The array's items can still be changed, but this method never does.- The standard library also has
parts.last, which gives the same item. The lesson indexes by hand to practise the arithmetic.
Tracing an example
For "photo.jpeg", split("\\.") gives two parts, photo and jpeg, so the last index is 1 and the method returns "jpeg". For "archive.tar.gz", the parts are archive, tar and gz, the last index is 2, and the result is "gz".
Edge cases and common mistakes
- Splitting with
".". The unescaped dot matches every character, so"a.b".split(".")is an empty array, and readingparts(parts.length - 1)from it throws an exception. - Reading index
parts.length. The last valid index is one less than the length. Indexparts.lengthis out of range. - Taking the first part. For
"archive.tar.gz",parts(0)is"archive", the name without its extension. - Trailing dots. Scala's
splitdrops empty pieces at the end, so"a.b.".split("\\.")is[a, b]and the method would return"b". The lesson's constraints exclude names that end with a dot.
Testing
"photo.jpeg"gives"jpeg"."archive.tar.gz"gives"gz"."README.MD"gives"MD".
