Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Sort by Extension by hasanibnmansoor
from typing import List
def sort_by_ext(files: List[str]) -> List[str]:
# your code here
# files = sorted(files,key=lambda x: (-x.startswith("."), x.split(".")[-1], ))
w_o_name = []
w_file_name = []
for file in files:
if file.startswith(".") and file.count(".") == 1:
w_o_name.append(file)
else:
w_file_name.append(file)
w_file_name = sorted(w_file_name,key=lambda x: (x.split(".")[-1], x.split(".")[0]))
print("w_file_name",w_file_name)
w_o_name = sorted(w_o_name)
print("w_o_name",w_o_name)
print("*"*10)
return w_o_name + w_file_name
if __name__ == '__main__':
print("Example:")
print(sort_by_ext(['1.cad', '1.bat', '1.aa', '.aa.doc']))
# 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!")
April 13, 2021