diff options
Diffstat (limited to 'challenge-074/roger-bell-west/python')
| -rwxr-xr-x | challenge-074/roger-bell-west/python/ch-1.py | 27 | ||||
| -rwxr-xr-x | challenge-074/roger-bell-west/python/ch-2.py | 28 |
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-074/roger-bell-west/python/ch-1.py b/challenge-074/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..483a169ff8 --- /dev/null +++ b/challenge-074/roger-bell-west/python/ch-1.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +import unittest + +def majority(list): + s=dict() + for x in list: + s.setdefault(x,0) + s[x] += 1 + m=max(s.values()) + if m > int(len(list)/2): + q=dict() + for x in s.keys(): + q[s[x]]=x + return q[m] + else: + return -1 + +class TestMajority(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(majority((1, 2, 2, 3, 2, 4, 2)),2,'example 1') + + def test_ex2(self): + self.assertEqual(majority((1, 3, 1, 2, 4, 5)),-1,'example 2') + +unittest.main() diff --git a/challenge-074/roger-bell-west/python/ch-2.py b/challenge-074/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..cd065d950b --- /dev/null +++ b/challenge-074/roger-bell-west/python/ch-2.py @@ -0,0 +1,28 @@ +#! /usr/bin/python3 + +import unittest + +def fnr(i): + s=dict() + ls=list() + o=list() + for c in list(i): + ls.append(c) + s.setdefault(c,0) + s[c] += 1 + ls=[x for x in ls if s[x]<2] + if len(ls)>0: + o.append(ls[len(ls)-1]) + else: + o.append('#') + return ''.join(o) + +class TestMajority(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(fnr('ababc'),'abb#c','example 1') + + def test_ex2(self): + self.assertEqual(fnr('xyzzyx'),'xyzyx#','example 2') + +unittest.main() |
