Skip to content

Commit 0f8ea82

Browse files
authored
Merge pull request #2 from adilrizvi/iterable-input
iterable input for commonprefix
2 parents 4075f17 + 1def9c4 commit 0f8ea82

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

idioms.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,26 @@ def flatten(iterables):
159159
else:
160160
yield i
161161

162-
def commonprefix(*instrings):
162+
def commonprefix(iterable, ignore_case=False):
163163
""" Common prefix of input strings using OrderedDict
164164
165-
>>> commonprefix('Python','Pythonista')
165+
>>> commonprefix(['Python','Pythonista'])
166166
'Python'
167-
>>> commonprefix('Persia','Person','Perl','Perlite')
167+
>>> commonprefix(('Persia','Person','Perl','Perlite'))
168168
'Per'
169-
>>> commonprefix('Batman', 'Battleship','Batista','Bat')
170-
'Bat'
171-
169+
>>> commonprefix(['rambo', 'Rambo'], ignore_case=True)
170+
'rambo'
172171
"""
173172

174173
d = collections.OrderedDict()
174+
175+
if isinstance(iterable, collections.Iterable):
176+
instrings = iterable
177+
else:
178+
raise Exception("Only Iterables are allowed as an Input")
179+
180+
# adjusting input strings according to the ignore_case option
181+
instrings = map(str.lower, instrings) if ignore_case else instrings
175182

176183
for instring in instrings:
177184
for idx,char in enumerate(instring):

0 commit comments

Comments
 (0)