Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear and easy solution in Clear category for Sort by Extension by siarhei-karko
from typing import List
def sort_by_ext(files: List[str]) -> List[str]:
return sorted(sorted(files), key=lambda x: x[x.rfind('.'):] if 0 < x.rfind('.') < len(x) else x[:x.rfind('.')])
if __name__ == '__main__':
print("Example:")
print(sort_by_ext(["no.name.",".config","green.bat","345.bin","format.c","my.doc","1.exe","best.test.exe"]))
print("result:", [".config","no.name.","green.bat","345.bin","format.c","my.doc","1.exe","best.test.exe"])
# # These "asserts" are used for self-checking and not for an auto-testing
assert sort_by_ext(["no.name.",".config","green.bat","345.bin","format.c","my.doc","1.exe","best.test.exe"]) == [".config","no.name.","green.bat","345.bin","format.c","my.doc","1.exe","best.test.exe"]
assert sort_by_ext(['1.cad', '2.bat', '1.aa', '1.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!")
April 20, 2021
Comments: