Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
4 lines with detailed explanation solution in Clear category for Sort by Extension by tkachuk.constantine
from typing import List
def sort_by_ext(files: List[str]) -> List[str]:
# Sorting row list alphabetically.
sorted_files = sorted(files)
# Sorting based on extenstion. Within the same extension alphabetical order remains!
sorted_files = sorted(sorted_files, key=lambda x: x[x.rfind('.'):])
# Creating new list with No Exstantion file names excluding them from the sorted list
# It is important that alphabetical order remains in new list
files_no_ext = [sorted_files.pop(sorted_files.index(file)) for file in sorted_files
if file.rfind('.') == -1 or len(file[file.rfind('.'):]) > 4]
# Creating new list with No File Name files excluding them from the sorted list
# It is important that alphabetical order remains in new list
files_no_name = [sorted_files.pop(sorted_files.index(file)) for file in sorted_files
if file.rfind('.') == 0 and len(file[file.rfind('.'):]) <= 4]
return files_no_name + files_no_ext + sorted_files
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!")
May 4, 2021