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 delahere
def sort_by_ext(files: list[str]) -> list[str]:
# your code here
#
# The key to this one is in clearly understanding the requirements.
#
# Separate the list into sublists of the three categories
# specified:
# - files without a name (extension only)
# - files that are name only, without an extension,
# including .config
# - files that have a name and extension
# Sort each sublist separatelty, first by extension and then by name if the file includes both
# Concatenate the lists together, no name, name only and both name and extension
# Return the list.
#
#
print(f'unsorted: {files}')
#
# Remove files without a name
no_name = []
name_only = []
name_plus_extension = []
for f in files:
#
# Treat the special case for .config (which is not an extension)
if f == ".config":
name_only.append(f)
continue
#
# handle the other name only cases
if f.count(".") == 0:
name_only.append(f)
continue
#
# Handle the cases in which there is only an extension
# (leading period and no other periods)
#
if f[0] == "." and f.count(".") == 1:
no_name.append(f)
continue
#
# Everything else has a name and an extension.
name_plus_extension.append(f)
#
# Before sorting the sublists, the sublist with names and extensions
# needs to be sorted first by extension and then by name.
# To do this, create a list in which the name and extension are reversed
# and separated by a space. The space will later serve as a delimiter
# which can safely be used because spaces are guaranteed to not be part
# of the data.
# Then sort the list of extensions + names.
# Then go through the list, split the extension and name, using a space
# as the delimiter, and add them to a new list of files that include names
# plus extensions.
ext_name_list = []
for f in name_plus_extension:
# Find the last period and use that to slice the file into a
# name and extension. Then concatenate the extension, a space, and the
# name together and append them to the extension first list.
pinx = f.rfind(".")
name = f[:pinx]
ext = f[pinx+1:]
ext_name_list.append(ext + " " + name)
ext_name_list.sort()
#
# Re-initialize the name plus extension list to empty.
#
name_plus_extension = []
for f in ext_name_list:
#
# Reverse the order of the extension and name by splitting the
# element, concatenating the name and extension together and
# append the result to the name plus extension sublist.
#
ext, name = f.split(" ")
name_plus_extension.append(name + "." + ext)
#
# Sort the other sublists and concatenate the sorted sublists together.
no_name.sort()
name_only.sort()
files_sorted = no_name + name_only + name_plus_extension
return files_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!")
April 18, 2023
Comments: