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 dig
def extension (string):
ext, i = '', 1
if '.' not in string or string[-1]=='.' or (string[0]=='.' and string.count('.')==1): return ''
while string[-i]!='.':
ext=string[-i]+ext
i+=1
return ext
def name(string):
Len_ext=len(extension (string))
return string[:-Len_ext]
def sort_by_ext(files: list[str]) -> list[str]:
file_sorted= sorted(files, key=lambda string: (extension(string), name(string)), reverse= False)
return file_sorted
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. 3, 2023
Comments: