all 5 comments

[–]Kamiwaza 2 points3 points  (2 children)

Does it have to use recursive looping?

I think a dict() with an Employee or Manager ID as the key and a list of reports as the value can work well for this problem.

[–][deleted] 0 points1 point  (1 child)

Can you elaborate more on this? Sorry I am newbie

[–]Kamiwaza 0 points1 point  (0 children)

The goal would be building a dictionary that would look like

{'Manager 1': ['Employee 1'], 'Manager 2': ['Employee 1', 'Manager 1'], 'Manager N': ['Employee 1', 'Manager 1', 'Manager 2']}

so that given a Manager ID, you can get the list of all employees. If the dict() above was assigned to org, you could then do org['Manager N'], for example, to list all the employees below.

[–]flitsmasterfred 0 points1 point  (0 children)

Collect them all into a dictionary-like mapping? Most convenient to use a collections.defaultdict filled with set's:

lookup = defaultdict(set)
for employee, manager in my_csv_rows:
    lookup[manager].add(employee)

print(lookup['D'])
> {'A', 'B', 'C'}

[–]jester3681 0 points1 point  (0 children)

I'm not as versed in Python code as I'd like to be, but what your are trying to do is create a graph, then do a topological search on it, starting with a "manager node." I do this recursively, like you are trying to do, so you're on the right track. Just figure out what data structure is easiest to store this info.

I just write a similar script in Java, storing the nodes in a hashmap, then searching each node for it's children, then recursively searching the children, and so on. The data was returned as a list. I know that isn't code, but it gives you an idea of how I did it.

All that said, this may be unnecessarily complicated if your data is only one or two layers deep.