Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sort by Extension Solution solution in Clear category for Sort by Extension by arogers23
from typing import List
#Seperating the scripts functions into two classes allows for a cleaner approach
#to solving the problem.
def extension(file):
#Using the split function allows for seperation of the file names and extensions
split = file.split('.')
#If the file names or extensions are empty, the if statement checks if one, both
#or none are true.
if('' == split[0] and len(split) == 2):
split = [''.join(split[1:]), '']
return split[::-1]
#With the class extension performing the split, and name/extension check function
#The sorted values can be returned.
def sort_by_ext(files: List[str]) -> List[str]:
return sorted(files, key=extension)
if __name__ == '__main__':
print("Example:")
print(sort_by_ext(['1.cad', '1.bat', '1.aa']))
# These "asserts" are used for self-checking and not for an auto-testing
assert sort_by_ext(['1.cad', '1.bat', '1.aa']) == ['1.aa', '1.bat', '1.cad']
assert sort_by_ext(['1.cad', '1.bat', '1.aa', '2.bat']) == ['1.aa', '1.bat', '2.bat', '1.cad']
assert sort_by_ext(['1.cad', '1.bat', '1.aa', '.bat']) == ['.bat', '1.aa', '1.bat', '1.cad']
assert sort_by_ext(['1.cad', '1.bat', '.aa', '.bat']) == ['.aa', '.bat', '1.bat', '1.cad']
assert sort_by_ext(['1.cad', '1.', '1.aa']) == ['1.', '1.aa', '1.cad']
assert sort_by_ext(['1.cad', '1.bat', '1.aa', '1.aa.doc']) == ['1.aa', '1.bat', '1.cad', '1.aa.doc']
assert sort_by_ext(['1.cad', '1.bat', '1.aa', '.aa.doc']) == ['1.aa', '1.bat', '1.cad', '.aa.doc']
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 5, 2021