File tree 1 file changed +22
-1
lines changed 1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -158,7 +158,28 @@ def flatten(iterables):
158
158
for j in flatten (i ): yield j
159
159
else :
160
160
yield i
161
-
161
+
162
+ def commonprefix (* instrings ):
163
+ """ Common prefix of input strings using OrderedDict
164
+
165
+ >>> commonprefix('Python','Pythonista')
166
+ 'Python'
167
+ >>> commonprefix('Persia','Person','Perl','Perlite')
168
+ 'Per'
169
+ >>> commonprefix('Batman', 'Battleship','Batista','Bat')
170
+ 'Bat'
171
+
172
+ """
173
+
174
+ d = collections .OrderedDict ()
175
+
176
+ for instring in instrings :
177
+ for idx ,char in enumerate (instring ):
178
+ # Make sure index is added into key
179
+ d [(char , idx )] = d .get ((char ,idx ), 0 ) + 1
180
+
181
+ # Return prefix of keys where value == length(instrings)
182
+ return '' .join ([k [0 ] for k in itertools .takewhile (lambda x : d [x ] == len (instrings ), d )])
162
183
163
184
if __name__ == "__main__" :
164
185
import doctest
You can’t perform that action at this time.
0 commit comments