Advertisement
Guest User

Untitled

a guest
May 6th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1.  
  2. #Filename cannot be an empty string;
  3. #Files without extensions should go before the files with extensions;
  4. #Filename ".config" has an empty extenstion and name ".config";
  5. #Filename "config." has an empty extenstion and name "config.";
  6. #Filename "table.imp.xls" has an extesntion "xls" and name "table.imp";
  7. #Filename ".imp.xls" has extension "xls" and name ".imp".
  8.  
  9. from typing import List
  10. import operator
  11.  
  12. def sort_by_ext(files: List[str]) -> List[str]:
  13. print(files)
  14. X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
  15. touplelist = []
  16. restlist1 = []
  17. for i in files:
  18. touplelist.append(tuple(i.split(".")))
  19. print(touplelist, "Zwykła lista")
  20. counter = 0
  21. for i in touplelist:
  22. if i[0] == "":
  23. restlist1.append(i)
  24. touplelist.pop(counter)
  25. counter += 1
  26. else:
  27. counter += 1
  28. continue
  29. #touplelist = sorted(touplelist, key=lambda tup: tup[1])
  30. touplelist = sorted(touplelist, key=operator.itemgetter(1, 0))
  31. print(touplelist, " ------------WYNIK")
  32. touplelist = restlist1 + touplelist
  33. Output = []
  34. restlist2 = []
  35. for i in touplelist:
  36. print(len(i))
  37. if len(i) < 3:
  38. Output.append(i[0]+"."+i[1])
  39. else:
  40. restlist2.append(i[0]+"."+i[1]+"."+i[2])
  41. print(Output, " OUT")
  42. if restlist2 != "":
  43. return Output + restlist2
  44. else:
  45. return Output
  46.  
  47.  
  48.  
  49. if __name__ == '__main__':
  50.  
  51. # These "asserts" are used for self-checking and not for an auto-testing
  52. assert sort_by_ext(['1.cad', '1.bat', '1.aa']) == ['1.aa', '1.bat', '1.cad']
  53. assert sort_by_ext(['1.cad', '1.bat', '1.aa', '2.bat']) == ['1.aa', '1.bat', '2.bat', '1.cad']
  54. assert sort_by_ext(['1.cad', '1.bat', '1.aa', '.bat']) == ['.bat', '1.aa', '1.bat', '1.cad']
  55. assert sort_by_ext(['1.cad', '1.bat', '.aa', '.bat']) == ['.aa', '.bat', '1.bat', '1.cad']
  56. assert sort_by_ext(['1.cad', '1.', '1.aa']) == ['1.', '1.aa', '1.cad']
  57. assert sort_by_ext(['1.cad', '1.bat', '1.aa', '1.aa.doc']) == ['1.aa', '1.bat', '1.cad', '1.aa.doc']
  58. assert sort_by_ext(['1.cad', '1.bat', '1.aa', '.aa.doc']) == ['1.aa', '1.bat', '1.cad', '.aa.doc']
  59. assert sort_by_ext([".config", "my.doc", "1.exe", "345.bin", "green.bat", "format.c", "no.name.", "best.test.exe"])
  60. print("Coding complete? Click 'Check' to earn cool rewards!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement