Page MenuHomePhabricator
Paste P1171

test python
ActivePublic

Authored by aarwine on Jun 5 2014, 12:00 AM.
Tags
None
Referenced Files
F163443: test_python
Jun 5 2014, 12:00 AM
Subscribers
None
#!/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)

Event Timeline

aarwine changed the title of this paste from untitled to test python.
aarwine updated the paste's language from autodetect to python.