-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathez_cppcodesort.py
More file actions
146 lines (119 loc) · 3.1 KB
/
ez_cppcodesort.py
File metadata and controls
146 lines (119 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import itertools
INFILE = "/home/elijah/temp2/codehelp/src"
lines = []
with open(INFILE, "r") as o:
for line in o:
line = line.lstrip(" \t")
line = line.rstrip(" \t\n\r")
line = line.rstrip("\\")
lines.append(line)
# print (lines)
assert(len(lines) > 0)
resultgroups = []
for l in lines:
sl = l.split("/")
name = sl[-1]
level = 0
group = resultgroups
while level < len(sl):
if level + 1 == len(sl):
break
prevgroup = group
for ngpair in group:
if len(ngpair) == 1:
continue
if ngpair[0] == sl[level]:
group = ngpair[1]
break
# print (prevgroup)
if prevgroup == group:
for ll in range(level, len(sl)-1):
ngpairnew = (sl[ll], [])
group.append(ngpairnew)
group = ngpairnew[1]
break
level += 1
group.append(name)
# for r in resultgroups[1]:
# print r
def recuprint0(group, lvl):
for ng in group:
if isinstance(ng, tuple):
print " "*lvl + "/" + ng[0] + ""
recuprint0(ng[1], lvl+1)
else:
print " "*lvl + ng
def recuprint1(group, cat):
items = []
for ng in group:
if isinstance(ng, tuple) == False:
items.append(ng)
if items != []:
print cat
for i in items:
print " "*4, i
for ng in group:
if isinstance(ng, tuple):
recuprint1(ng[1], cat + "/" + ng[0])
def recuprint2(group, lvl, cat):
items = []
for ng in group:
if isinstance(ng, tuple) == False:
items.append(ng)
print lvl*" " + cat
for i in items:
print " "*4, i, " - "
for ng in group:
if isinstance(ng, tuple):
recuprint2(ng[1], lvl+1, ng[0])
# recuprint(resultgroups, 0)
# print("\n\n")
def cmpcpph(a, b):
ax = a.split(".")
bx = b.split(".")
if len(ax) > 1 and len(bx) > 1:
return ax[0] == bx[0]
if len(ax) == 1 and len(bx) == 1:
return a == b
return False
# clps = [ "a.h", "b.h", "abc.cpp", "bas.c", "a.f", "d.bn" ]
# cmpresult = [ cmpcpph(clps[0], clpsn) for clpsn in clps[1:] ]
# cmpresult = [ True ] + cmpresult
# print cmpresult
# pairs = itertools.compress(clps, cmpresult)
# other = itertools.compress(clps, [not elem for elem in cmpresult])
def recucollapse(group):
clps = []
for ng in group:
if isinstance(ng, tuple) == False:
clps.append(ng)
rslt = []
while len(clps) > 0:
cmpresult = [ cmpcpph(clps[0], clpsn) for clpsn in clps[1:] ]
cmpresult = [ True ] + cmpresult
pairs = list(itertools.compress(clps, cmpresult))
rslt.append(", ".join(pairs))
clps = list(itertools.compress(clps, [not elem for elem in cmpresult]))
rlg = []
for ng in group:
if isinstance(ng, tuple):
rlgi = (ng[0], recucollapse(ng[1]))
rlg.append(rlgi)
return rslt + rlg
def recusort(group):
rlg = []
rl = []
for ng in group:
if isinstance(ng, tuple):
rlg.append(ng)
else:
rl.append(ng)
if len(rlg) > 0:
rlg.sort(key=lambda x:x[0])
for i in range(len(rlg)):
rlg[i] = (rlg[i][0], recusort(rlg[i][1]))
rl.sort()
return rlg + rl
resultgroups = recusort(resultgroups)
resultgroups = recucollapse(resultgroups)
recuprint2(resultgroups, 0, "")