From 391fd8db38048295d87dbd1716d9756546fc0813 Mon Sep 17 00:00:00 2001 From: ealvar3z <55966724+ealvar3z@users.noreply.github.com> Date: Thu, 17 Nov 2022 21:22:58 +0000 Subject: pwc191 Go and Python solutions --- challenge-191/ealvar3z/python/ch-1.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-191/ealvar3z/python/ch-1.py (limited to 'challenge-191/ealvar3z/python/ch-1.py') diff --git a/challenge-191/ealvar3z/python/ch-1.py b/challenge-191/ealvar3z/python/ch-1.py new file mode 100644 index 0000000000..6785f8112e --- /dev/null +++ b/challenge-191/ealvar3z/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +from functools import reduce +import unittest + + +class TestSolution(unittest.TestCase): + cases = [ + [1, 2, 3, 4], + [1, 2, 0, 5], + [2, 6, 3, 1], + [4, 5, 2, 3], + ] + + def twice_largest(self, lst): + s = sorted(lst) + head = reduce(lambda x, y: x+y, s[:-1]) + tail = s[-1] + + if head > tail: + return -1 + else: + return 1 + + def test_twice_largest(self): + self.assertEqual(self.twice_largest(self.cases[0]), -1, 'example 1') + self.assertEqual(self.twice_largest(self.cases[1]), 1, 'example 2') + self.assertEqual(self.twice_largest(self.cases[2]), 1, 'example 3') + self.assertEqual(self.twice_largest(self.cases[3]), -1, 'example 4') + + +if __name__ == "__main__": + unittest.main(verbosity=True) -- cgit