diff options
| author | Roger Bell_West <roger@firedrake.org> | 2021-01-12 14:28:49 +0000 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2021-01-12 14:28:49 +0000 |
| commit | 56b54d2ab2b254a3dcd11bb0c5e0da2df987e783 (patch) | |
| tree | a5f4051d198c6a092bf82e1777d23a06dcfa7e6e /challenge-095/roger-bell-west/python | |
| parent | b00c8c4f9ff06c18683a5e5380b03585f3301e43 (diff) | |
| download | perlweeklychallenge-club-56b54d2ab2b254a3dcd11bb0c5e0da2df987e783.tar.gz perlweeklychallenge-club-56b54d2ab2b254a3dcd11bb0c5e0da2df987e783.tar.bz2 perlweeklychallenge-club-56b54d2ab2b254a3dcd11bb0c5e0da2df987e783.zip | |
Solutions for challenge #095
Diffstat (limited to 'challenge-095/roger-bell-west/python')
| -rwxr-xr-x | challenge-095/roger-bell-west/python/ch-1.py | 23 | ||||
| -rwxr-xr-x | challenge-095/roger-bell-west/python/ch-2.py | 53 |
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-095/roger-bell-west/python/ch-1.py b/challenge-095/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..e188b3f325 --- /dev/null +++ b/challenge-095/roger-bell-west/python/ch-1.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 +import unittest + +def pn(n): + ns='{0}'.format(n) + nr=ns[::-1] + if (nr == ns): + return 1 + else: + return 0 + +class TestPn(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(pn(1221),1,'example 1'); + + def test_ex2(self): + self.assertEqual(pn(-101),0,'example 2'); + + def test_ex3(self): + self.assertEqual(pn(90),0,'example 3'); + +unittest.main() diff --git a/challenge-095/roger-bell-west/python/ch-2.py b/challenge-095/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..8b3b63b97f --- /dev/null +++ b/challenge-095/roger-bell-west/python/ch-2.py @@ -0,0 +1,53 @@ +#! /usr/bin/python3 +import unittest + +class Stack: + + def __init__(self): + self.stack=list() + + def push(self,op): + self.stack.append(op) + + def pop(self): + if len(self.stack)==0: + raise Exception("Insufficient elements for pop") + return self.stack.pop() + + def top(self): + if len(self.stack)==0: + raise Exception("Insufficient elements for top") + return self.stack[len(self.stack)-1] + + def min(self): + if len(self.stack)==0: + raise Exception("Insufficient elements for min") + return min(self.stack) + +class TestStack(unittest.TestCase): + + def test_ex1(self): + stack=Stack() + stack.push(2) + stack.push(-1) + stack.push(0) + self.assertEqual(stack.pop(),0,'example 1') + + def test_ex2(self): + stack=Stack() + stack.push(2) + stack.push(-1) + stack.push(0) + stack.pop() + self.assertEqual(stack.top(),-1,'example 2') + + def test_ex3(self): + stack=Stack() + stack.push(2) + stack.push(-1) + stack.push(0) + stack.pop() + stack.push(0) + self.assertEqual(stack.min(),-1,'example 3') + +unittest.main() |
