The input file contains a list of file paths. suggest an algorithm for sorting the hierarchy as shown below
Enter
A/file1 A/B/C/D/file3 A/B/file1 A/B/file2 A/B/C/D/file1 A/file2 A/W/X/Y/Z/file1 A/W/file1 A/W/X/file1 A/file3 A/B/C/file1 A/W/X/Y/file1 A/B/file2
Expected Result
A/file1 A/file2 A/file3 A/B/file1 A/B/file2 A/B/C/file1 A/B/C/D/file1 A/B/C/D/file3 A/W/file1 A/W/X/file1 A/W/X/Y/file1 A/W/X/Y/Z/file1
tried coding as shown below, the result does not fit as expected
import sys,os d1,d2 = '','' l1 = [ x for x in open(sys.argv[1])] s2 = sorted(l1,key = lambda x : len(x.split('/'))) for linE in s2: f1 = linE.strip('\n') d1 = os.path.dirname(f1) if d1 != d2 : print d2 = d1 print linE,
Current output
A/file1 A/file2 A/file3 A/B/file1 A/B/file2 A/W/file1 A/B/file2 A/W/X/file1 A/B/C/file1 A/B/C/D/file3 A/B/C/D/file1 A/W/X/Y/file1 A/W/X/Y/Z/file1
please help me with the algorithm do the same
source share