-
Notifications
You must be signed in to change notification settings - Fork 3
/
countemployees.py
94 lines (65 loc) · 2.23 KB
/
countemployees.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Count employees in an org chart.
Our organization has the following org chart::
Jane
Jessica Janet
Al Bob Jen Nick Nora
Henri
Let's make this chart::
>>> 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])
And test our counting function::
>>> henri.count_employees()
0
>>> nora.count_employees()
1
>>> jane.count_employees()
8
We provide a non-recursive version, let's make sure that gives the same
answer::
>>> jane.count_employees_nonrecursive()
8
"""
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.
"""
count = 0
for child in self.children:
count = 1 + count + child.count_employees()
return count
def count_employees_nonrecursive(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.
Non-recursive version.
"""
# Using a loop, also a fine way to solve this, although it's
# probably a little trickier and less elegant than the
# recursive version
count = 0
to_visit = [self]
while to_visit:
emp = to_visit.pop()
for child in emp.children:
count += 1
to_visit.append(child)
return count
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print ("\n*** ALL TESTS PASSED. YOU ARE A TREE GENIUS!\n")