Page MenuHomePhabricator

test_python

Authored By
aarwine
Jun 5 2014, 12:00 AM
Size
835 B
Referenced Files
None
Subscribers
None

test_python

#!/usr/bin/python2
def commonInLists(listOfLists):
# Input: List of lists
# Output: Single list of common elements
# Must take unknown number of lists
if len(listOfLists) == 0:
raise Exception('No.')
elif len(listOfLists) == 1:
return listOfLists[0]
elif len(listOfLists) == 2:
return list(set(listOfLists[0]).intersection(listOfLists[1]))
elif len(listOfLists) > 2:
result = set(listOfLists[0]).intersection(listOfLists[1])
for i in range(2, len(listOfLists)):
result = result.intersection(listOfLists[i])
return result
else:
raise Exception('How did I get here?')
a = ['1', '2', '3', '4', '5']
b = ['2', '3', '4', '5', '6']
c = ['3', '4', '5', '6', '7']
d = ['4', '5', '6', '7', '8']
foo = [a, b, c, d]
print commonInLists(foo)

File Metadata

Mime Type
text/plain; charset=utf-8
Storage Engine
amazon-s3
Storage Format
Raw Data
Storage Handle
phabricator/og/ty/epbqpewspop4wlku
Default Alt Text
test_python (835 B)

Event Timeline