Problem overview
The method fileExtension receives a file name, fileName, and returns its extension: the part after the last dot. A name can contain several dots, but only what follows the final one is the extension, and its letters are kept exactly as written. The name has at least one dot, doesn't start or end with a dot, and has no spaces.
For "photo.jpeg" the result is "jpeg". For "archive.tar.gz" it is "gz", not "tar.gz". For "README.MD" it is "MD", with the upper-case letters kept.
Approach
Cut the name at every dot, then take the last piece. The string method split does the cutting: it returns a list of the parts between the separators. Taking the last item of that list is then a matter of indexing, and a negative index counts from the end of a list just as it does for a string. The approach doesn't depend on how many dots there are, because the last part is always the one after the final dot.
Step by step
- Split the name at the dots.
fileName.split(".")returns a list, which the solution stores inparts. For"archive.tar.gz"the list is['archive', 'tar', 'gz'], and for"photo.jpeg"it is['photo', 'jpeg']. - Return the last item.
parts[-1]is the last item whatever the length of the list:'gz'for the first list,'jpeg'for the second.
The solution in Python
class Solution:
def fileExtension(self, fileName: str) -> str:
parts = fileName.split(".")
return parts[-1]
Some details worth knowing:
split(".")takes the separator as a string, and every occurrence is a cut. The separator itself doesn't appear in the parts.- The result is a list of strings. Indexing a list with
-1works the same way as indexing a string with-1. - Nothing is converted:
"README.MD".split(".")[-1]is"MD". If you wanted a lower-case extension, you would calllower()on it, but this lesson asks for the letters as they are. - The parts are new strings.
fileNameitself is not changed.
Other ways to get the same result
Python has string methods made for cutting at the last separator. "archive.tar.gz".rsplit(".", 1)[-1] and "archive.tar.gz".rpartition(".")[2] both give "gz". The lesson's two-step version with split is the clearest to read, and it works well here because file names are short. A name with a single dot is the simplest case: split gives two parts and the extension is the second.
Edge cases and common mistakes
- Several dots. Take the last part, not the second one. For
"archive.tar.gz",parts[1]would be"tar", which is wrong. - The first part by mistake.
parts[0]is the base name,"archive". - Splitting on the wrong separator.
split(",")orsplit(" ")finds nothing to cut and gives a list with the whole name as its only item. - Names the constraints rule out. A name ending with a dot, like
"a.b.", gives['a', 'b', ''], so the last item is an empty string. A name with no dot, like"noext", gives['noext'], and the code returns the whole name. The lesson doesn't pass either kind. - Changing case. Don't lower-case the extension.
"MD"must stay"MD".
Testing
"photo.jpeg"gives"jpeg"."archive.tar.gz"gives"gz"."README.MD"gives"MD".
