Using the following function I can generate some test data.
import random, string
a = list(string.ascii_lowercase)
def gen_test_data():
s = []
for i in xrange(15):
p = random.randint(1,3)
xs = [random.choice(a) for i in xrange(p)]
s.append(xs)
return s
This is my test data.
[
['a', 's'],
['f', 'c'],
['w', 'z'],
['z', 'p'],
['z', 'u', 'g'],
['v', 'q', 'w'],
['y', 'w'],
['d', 'x', 'i'],
['l', 'f', 's'],
['z', 'g'],
['h', 'x', 'k'],
['b'],
['t'],
['s', 'd', 'c'],
['s', 'w', 'd']
]
If a letter shares the list with another letter it is dependent on that letter, and any of that letters other dependencies. For example
x
is dependant on ['a','c','d','g','f','i','h','k','l','q','p','s','u','w','v','y', 'x','z']
These dependencies are also two way. k
is dependent on everything including x
but x
is not dependant on b
or t
. These can be placed in separate groups.
I need to divide the list into as MANY non dependant groups as possible.
Each group will be a set of all letters that the group depends on. Non dependent letters will be a set of one.
An example output to the one above is
[
['t'],
['b'],
['a','c','d','g','f','i','h','k','l','q','p','s','u','w','v','y', 'x','z']
]
I am trying to write a function to do this but can't figure out the right way to group everything correctly.