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 Pavellver
def sort_by_ext(files: list[str]) -> list[str]:
a = sorted([i for i in files if i[-1] == '.'])
b = sorted([i for i in files if i[0] == '.' and i.count('.') == 1])
c = sorted([(i[i.rfind('.'):], i[:i.rfind('.')]) for i in files if i not in a and i not in b])
d = b + a + [n[1]+n[0] for n in c]
return d
print("Example:")
print(sort_by_ext(["1.cad", "1.bat", "1.aa"]))
# These "asserts" are used for self-checking
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("The mission is done! Click 'Check Solution' to earn rewards!")
Feb. 26, 2023
Comments: