aboutsummaryrefslogtreecommitdiff
path: root/challenge-125/roger-bell-west/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-125/roger-bell-west/python/ch-2.py')
-rwxr-xr-xchallenge-125/roger-bell-west/python/ch-2.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-125/roger-bell-west/python/ch-2.py b/challenge-125/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..b03a09c4e0
--- /dev/null
+++ b/challenge-125/roger-bell-west/python/ch-2.py
@@ -0,0 +1,36 @@
+#! /usr/bin/python3
+
+import unittest
+
+from math import degrees,atan2
+
+def btd(tree):
+ st=len(tree)
+ depth=[0]*st
+ diameter=[0]*st
+ for i in range(st-1,-1,-1):
+ if tree[i] != 0:
+ a=i*2+1
+ b=a+1
+ if b < st:
+ depth[i]=1+max(depth[a],depth[b])
+ diameter[i]=max(
+ 1+depth[a]+depth[b],
+ diameter[a],
+ diameter[b]
+ )
+ else:
+ depth[i]=1
+ diameter[i]=1
+ return diameter[0]
+
+class TestBtd(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(btd([1,
+ 2,5,
+ 3,4,6,7,
+ 0,0,0,0,0,0,8,10,
+ 0,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0]),7,'example 1')
+
+unittest.main()