-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathcount_employees.py
58 lines (39 loc) · 1.27 KB
/
count_employees.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Node(object):
""" Node in a tree. """
def __init__(self, name, children=None):
self.name = name
self.children = children or []
def count_employees(self):
"""Return a count of how many employees this person manages.
Return a count of how many people that manager manages. This should
include *everyone* under them, not just people who directly report to
them.
>>> henri = Node("Henri")
>>> nora = Node("Nora", [henri])
>>> nick = Node("Nick")
>>> janet = Node("Janet", [nick, nora])
>>> al = Node("Al")
>>> bob = Node("Bob")
>>> jen = Node("Jen")
>>> jessica = Node("Jessica", [al, bob, jen])
>>> jane = Node("Jane", [jessica, janet])
>>> henri.count_employees()
0
>>> janet.count_employees()
3
>>> jessica.count_employees()
3
>>> jane.count_employees()
8
"""
if not self.children:
return 0
count = 0
for child in self.children:
count += 1 + child.count_employees()
return count
if __name__ == '__main__':
import doctest
results = doctest.testmod()
if results.failed == 0:
print("ALL TESTS PASSED!")