aboutsummaryrefslogtreecommitdiff
path: root/challenge-125/roger-bell-west/python
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2021-08-09 16:21:34 +0100
committerRoger Bell_West <roger@firedrake.org>2021-08-09 16:21:34 +0100
commitda9333b5b848624319742346d77d09cf35ec3e6a (patch)
tree91ae7c60908c82dbcc5ac1773a277ca590f79c3e /challenge-125/roger-bell-west/python
parentdae33f10b00beaf02883597401ede84ddcc3b77f (diff)
downloadperlweeklychallenge-club-da9333b5b848624319742346d77d09cf35ec3e6a.tar.gz
perlweeklychallenge-club-da9333b5b848624319742346d77d09cf35ec3e6a.tar.bz2
perlweeklychallenge-club-da9333b5b848624319742346d77d09cf35ec3e6a.zip
Solutions for challenge #125
Diffstat (limited to 'challenge-125/roger-bell-west/python')
-rwxr-xr-xchallenge-125/roger-bell-west/python/ch-1.py45
-rwxr-xr-xchallenge-125/roger-bell-west/python/ch-2.py36
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-125/roger-bell-west/python/ch-1.py b/challenge-125/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..4fab2618f2
--- /dev/null
+++ b/challenge-125/roger-bell-west/python/ch-1.py
@@ -0,0 +1,45 @@
+#! /usr/bin/python3
+
+import unittest
+from collections import deque
+
+def pt(n):
+ out=[]
+ tri=deque()
+ tri.append([3,4,5])
+ while len(tri)>0:
+ t=tri.popleft()
+ for i in range(3):
+ dm=divmod(n,t[i])
+ if dm[1]==0:
+ out.append([i*dm[0] for i in t])
+ if min(t) <= n:
+ tri.append([
+ t[0]-2*t[1]+2*t[2],
+ 2*t[0]-1*t[1]+2*t[2],
+ 2*t[0]-2*t[1]+3*t[2],
+ ])
+ tri.append([
+ t[0]+2*t[1]+2*t[2],
+ 2*t[0]+1*t[1]+2*t[2],
+ 2*t[0]+2*t[1]+3*t[2],
+ ])
+ tri.append([
+ -t[0]+2*t[1]+2*t[2],
+ -2*t[0]+1*t[1]+2*t[2],
+ -2*t[0]+2*t[1]+3*t[2],
+ ])
+ return out
+
+class TestPt(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(pt(5),[[3,4,5],[5,12,13]],'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(pt(13),[[5,12,13],[13,84,85]],'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(pt(1),[],'example 3')
+
+unittest.main()
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()